最近在用pandas时出现的错误:
目的是想要选择dataframe某列中出现最大值的行:
df_i_max = df_i.loc[df_i['queue_length'] == que_max]
df_i是我要操作的dataframe,此时报错了,报错码如下:
Traceback (most recent call last):
File "test1.py", line 13, in <module>
df_i_max = df_i.loc[df_i['queue_length'] == que_max]
File "F:\anaconda\lib\site-packages\pandas\core\ops.py", line 1676, in wrapper
raise ValueError("Can only compare identically-labeled "
ValueError: Can only compare identically-labeled Series objects
我以为索引出了问题,因为df_i是由其他dataframe筛选的,试了试其他的方法无果。
然后看到了
其他博主----链接
找出问题,原来我的que_max在循环里会迭代,是个变量。如果是定值的话就可以用==
按照链接里的方法修改后语句:
df_i_max = df_i.loc[df_i['queue_length'].isin(que_max)]
成功选取数据: