熊猫有问题,我想听听你的意见
我有这个数据框,我需要在其中获取最大值,代码就在下面,
df_stack=pd.DataFrame([[1.0, 2016.0, 'NonResidential', 'Hotel', 98101.0, 'DOWNTOWN',
47.6122, -122.33799, 1927.0, 57.85220900338872,
59.91269863912585],
[1.0, 2016.0, 'NonResidential', 'Hotel', 98101.0, 'DOWNTOWN',
47.61317, -122.33393, 1996.0, 55.82342114189166,
56.86951201265458],
[3.0, 2016.0, 'NonResidential', 'Hotel', 98101.0, 'DOWNTOWN',
47.61393, -122.3381, 1969.0, 76.68191235628086,
77.37931271575705],
[5.0, 2016.0, 'NonResidential', 'Hotel', 98101.0, 'DOWNTOWN',
47.61412, -122.33664, 1926.0, 68.53505428597694,
71.00764283155655],
[8.0, 2016.0, 'NonResidential', 'Hotel', 98121.0, 'DOWNTOWN',
47.61375, -122.34047, 1980.0, 67.01346098859122,
68.34485815906346]], columns=['OSEBuildingID', 'DataYear', 'BuildingType', 'PrimaryPropertyType',
'ZipCode', 'Neighborhood', 'Latitude', 'Longitude', 'YearBuilt',
'SourceEUI(KWm2)', 'SourceEUIWN(KWm2)' ])
当我运行下面的代码时:
df_stack[['OSEBuildingID',
'DataYear',
'BuildingType',
'PrimaryPropertyType',
'ZipCode', 'Neighborhood', 'Latitude', 'Longitude',
'YearBuilt', 'SourceEUI(KWm2)', 'SourceEUIWN(KWm2)']].groupby('OSEBuildingID').max()
我收到一个错误“AssertionError:”,如果您尝试此操作,您可能会收到相同的错误。
但是,当我评论这两列并再次运行代码时
df_stack[['OSEBuildingID',
'DataYear',
#'BuildingType',
#'PrimaryPropertyType',
'ZipCode', 'Neighborhood', 'Latitude', 'Longitude',
'YearBuilt', 'SourceEUI(KWm2)', 'SourceEUIWN(KWm2)']].groupby('OSEBuildingID').max()
我得到结果
DataYear ZipCode Neighborhood Latitude Longitude YearBuilt SourceEUI(KWm2) SourceEUIWN(KWm2)
OSEBuildingID
1.0 2016.0 98101.0 DOWNTOWN 47.61317 -122.33393 1996.0 57.852209 59.912699
3.0 2016.0 98101.0 DOWNTOWN 47.61393 -122.33810 1969.0 76.681912 77.379313
5.0 2016.0 98101.0 DOWNTOWN 47.61412 -122.33664 1926.0 68.535054 71.007643
8.0 2016.0 98121.0 DOWNTOWN 47.61375 -122.34047 1980.0 67.013461 68.344858
如果我用mean()替换max(),我可以取消注释这两行并毫无问题地运行代码。
这种行为只发生在 max() 和 min() 中,我只是测试 max、mean 和 min,
但我需要获得最大值。
如果可以帮忙的话谢谢。