groupby-TypeError'DataFrame'对象不是callab

2024-04-19 15:11:05 发布

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

新来的-我的第一次尝试似乎没问题,但这是我第二次使用熊猫。 在Windows7上使用Pandas0.12.0时,我从SQL中读取了2个数据帧 其中一个可以像预期的那样使用groupby,所以我确信我的问题不是语法。 但另一方面,当type(reddf)返回pandas.core.frame.DataFrame时,当尝试reddf.groupby('any column')时,我得到-最后几行-

    c:\python27\lib\site-packages\pandas\core\groupby.pyc in __init__(self, index, grouper,     name, level, sort)
   1197             # no level passed
   1198             if not isinstance(self.grouper, np.ndarray):
-> 1199                 self.grouper = self.index.map(self.grouper)
   1200                 if not (hasattr(self.grouper,"__len__") and \
   1201                    len(self.grouper) == len(self.index)):

c:\python27\lib\site-packages\pandas\algos.pyd in pandas.algos.arrmap_int64 (pandas\algos.c:62839)()

TypeError: 'DataFrame' object is not callable

我知道groupby可以,并且该列存在,所以在数据帧上还有一些其他的约束/条件,我只是不知道或忘记了。 那么是什么导致了这个错误呢?我该怎么办?我将来应该找什么?

请求的信息

print type(reddf.index)
<class 'pandas.core.index.Int64Index'>

print repr(reddf.index) 
Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], dtype=int64)

print type(reddf.index.map)
<type 'instancemethod'>

print repr(reddf.index.map)
<bound method Int64Index.map of Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], dtype=int64)>

Just in case
reddf gives
<class 'pandas.core.frame.DataFrame'>
Int64Index: 20 entries, 0 to 19
Data columns (total 24 columns):
AssetId                  20  non-null values
DateAdded                20  non-null values
ModelId                  20  non-null values
UsageTypeId              20  non-null values
DateAdded                20  non-null values
Name                     20  non-null values
NatureId                 20  non-null values
IsContainer              20  non-null values
SparePartNumber          8  non-null values
ProductNumber            19  non-null values
SupportCategoryOid       20  non-null values
SerialNumber             20  non-null values
IpAddress                20  non-null values
Description              20  non-null values
CustomsId                15  non-null values
AssetTag                 20  non-null values
ParentId                 5  non-null values
ManagementProcessorId    7  non-null values
OperatingSystem          20  non-null values
OsVersion                20  non-null values
SystemName               20  non-null values
LocationId               10  non-null values
RomVersion               20  non-null values
MacAddress               19  non-null values
dtypes: bool(1), datetime64[ns](2), float64(3), int64(5), object(13)

尤其是在执行reddf.groupby('ModelId')时出错。 谢谢

感谢大家, 重复的字段名导致了这个问题,我不敢相信我以前没有注意到 最后的评论。

现在,我不明白.index输出是如何消除其他问题的,您能详细说明一下吗?如果索引丢失了,groupby不应该能够正常工作,为什么不呢?只要找一个简短的解释,如果你指向代码,就可以了。 谢谢你们的帮助,伙计们。


Tags: coreselfmappandasindextypenullvalues
2条回答

仅供参考,重复的列名不应再导致此错误。如果您使用的是最新的panda,则此错误是由其他原因造成的。

见:https://github.com/pandas-dev/pandas/pull/8210

是由“DateAdded”列的重复引起的。把它改名,你就可以走了。

相关问题 更多 >