首页 > 解决方案 > 在 python 中使用 panda 中的 max 函数

问题描述

我正在使用 python 和 panda 在 Juypternotebook 上做一个在线教程,当我运行以下代码时,我遇到了这个错误。

抱歉,这是我第一次在堆栈溢出上发帖,所以请原谅我违反规则...

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

import warnings
warnings.filterwarnings('ignore')

%matplotlib inline

# reading the csv file
titanic = pd.read_csv("titanic.csv") 

titanic_class = titanic.groupby("Pclass")   

titanic_class.get_group(1)
titanic_class.max()
    AssertionError                            Traceback (most recent call last)
    <ipython-input-26-4d1be28a55cb> in <module>
          1 #max ticket fare paid
    ----> 2 titanic_class.max()

    ~\anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in f(self, **kwargs)
       1369                 # try a cython aggregation if we can
       1370                 try:
    -> 1371                     return self._cython_agg_general(alias, alt=npfunc, **kwargs)
       1372                 except DataError:
       1373                     pass

    ~\anaconda3\lib\site-packages\pandas\core\groupby\generic.py in _cython_agg_general(self, how, alt, numeric_only, min_count)
        992     ) -> DataFrame:
        993         agg_blocks, agg_items = self._cython_agg_blocks(
    --> 994             how, alt=alt, numeric_only=numeric_only, min_count=min_count
        995         )
        996         return self._wrap_agged_blocks(agg_blocks, items=agg_items)

    ~\anaconda3\lib\site-packages\pandas\core\groupby\generic.py in _cython_agg_blocks(self, how, alt, numeric_only, min_count)
       1098             # Clean up the mess left over from split blocks.
       1099             for locs, result in zip(split_items, split_frames):
    -> 1100                 assert len(locs) == result.shape[1]
       1101                 for i, loc in enumerate(locs):
       1102                     new_items.append(np.array([loc], dtype=locs.dtype))

    AssertionError: 

有人可以告诉我有什么问题吗?titanic_class.sum() 和 titanic_class.mean() 工作没有任何错误。

标签: python-3.xpandas-groupby

解决方案


excel文件的最后一列有字母。一旦我删除它们, max 函数就起作用了。


推荐阅读