首页 > 解决方案 > KeyError: 0 in pandas

问题描述

waste['GDP ($M)'] - my column in a table Table 'waste' consist of 161 rows Error is on the 3 line (with condition). It the table 'True" values are exist, but I can't understand where is the problem. What should I change in my code?

Also, I changed values in a table from Scientific Notation to float, due to this my NaN values changed to "nan"

column = pd.isna(waste['GDP ($M)'])
gdp_nan = []
for row in range(len(waste)):
    if column[row] == True:
        gdp_nan.insert(row, row)
print(gdp_nan)

enter image description here

标签: pythonpandasdataframe

解决方案


As it was said in the comments, change range(2, len(waste)) Then, I had KeyError 21, which means that 21 record doesn't exist. You should reindex your dataframe.

waste = waste.reset_index(drop = True)

right in this way, then loop will work well


推荐阅读