我使用read_csv
並希望刪除一些具有特定值的行,我可以在單獨的行中做到這一點,但是當我想在同一行中調用它們時,它將返回一個NoneType
錯誤。熊貓設置多行數據幀
我的第一個代碼:
census_df = pd.read_csv('census.csv')
census_df.drop(census_df['SUMLEV'] == 40, inplace=True)
我想做的事:
census_df = (pd.read_csv('census.csv')
.drop(census_df['SUMLEV'] == 40, inplace=True))
它返回一個錯誤:
TypeError: 'NoneType' object is not subscriptable
是的,我知道布爾方式,我只是想在輸入乾淨代碼的數據框時應用它。 順便說一句,布爾索引比.drop更好嗎? – paulmassimo
我測試它,並與滴更慢2倍。 'c':['SUMLEV':[40,2,3], 'B':[4,5,6], 'C':[7,8,9], 'D' :[1,3,5], 'E':[5,3,6], 'F':[7,4,3]}) print(census_df)'然後'%timeit(census_df [(census_df ['SUMLEV']!= 40)]) 1000循環,最好爲3:每循環388微秒和'%timeit(census_df.drop(census_df [(census_df ['SUMLEV'] == 40)] .index)) 1000循環,最好3:每循環723微秒' – jezrael
非常感謝 – paulmassimo