如何使用Pandas在?

2024-09-28 22:02:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我经常对熊猫切片操作感到困惑,比如

import pandas as pd
raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'], 
    'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'], 
    'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'], 
    'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],
    'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'name', 'preTestScore', 'postTestScore'])

def get_stats(group):
    return {'min': group.min(), 'max': group.max(), 'count': group.count(), 'mean': group.mean()}
bins = [0, 25, 50, 75, 100]
group_names = ['Low', 'Okay', 'Good', 'Great']
df['categories'] = pd.cut(df['postTestScore'], bins, labels=group_names)
des = df['postTestScore'].groupby(df['categories']).apply(get_stats).unstack()
des.at['Good','mean']

我得到了:

TypeError Traceback (most recent call last) pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

TypeError: an integer is required

在处理上述异常时,发生了另一个异常:

KeyError Traceback (most recent call last) in () ----> 1 des.at['Good','mean']

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in getitem(self, key) 1867 1868 key = self._convert_key(key) -> 1869 return self.obj._get_value(*key, takeable=self._takeable) 1870 1871 def setitem(self, key, value):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _get_value(self, index, col, takeable) 1983 1984 try: -> 1985 return engine.get_value(series._values, index) 1986 except (TypeError, ValueError): 1987

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

KeyError: 'Good'

我该怎么做?在

提前谢谢。在


Tags: keyinselfpandasdfgetindexvalue
2条回答

由于分类索引,它不起作用:

des.index
# Out[322]: CategoricalIndex(['Low', 'Okay', 'Good', 'Great'], categories=['Low', 'Okay', 'Good', 'Great'], ordered=True, name='categories', dtype='category')

试着换成这样:

^{pr2}$

线路有问题

des = df['postTestScore'].groupby(df['categories']).apply(get_stats).unstack()

通过“postTestScroe”进行分组后,您将得到“Series”而不是“DataFrame”,如下所示。在

enter image description here

现在,当您尝试使用DataFrame des访问标量标签时,.at“无法识别标签‘Good’,因为它不存在于Series中。在

^{pr2}$

只需尝试打印des打印,您将看到结果系列。在

           count   max   mean   min
categories
Low           2.0  25.0  25.00  25.0
Okay          0.0   NaN    NaN   NaN
Good          8.0  70.0  63.75  57.0
Great         2.0  94.0  94.00  94.0

相关问题 更多 >