我使用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