***我在Pandas上遇到错误,请帮忙
df.reset_index(drop=True)
ind = list(df[df['BasePay'] == 0.0].index)
df.drop(df.index[ind])
错误输出为-
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-88-9ca6ffe769c8> in <module>
1 df.reset_index(drop=True)
2 ind = list(df[df['BasePay'] == 0.0].index)
----> 3 df.drop(df.index[ind])
~/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in __getitem__(self, key)
4289
4290 key = com.values_from_object(key)
-> 4291 result = getitem(key)
4292 if not is_scalar(result):
4293 return promote(result)
IndexError: index 148043 is out of bounds for axis 0 with size 148043
---------------------------------------------------------------------------
处理此数据集
https://www.kaggle.com/kaggle/sf-salaries
答案 0 :(得分:1)
如果我正确理解,您正在尝试删除“ BasePay”列为0.0的行。 您可以简单地使用boolean indexing进行此操作,以保持所需的行像这样:
newDF = df[df['BasePay'] != 0.0]