Skip to main content
 首页 » 编程设计

python之Pandas 设置多行数据框

2024年10月17日8exmyth

我使用 read_csv 并想删除一些具有特定值的行,我可以在不同的行中做得很好,但是当我想在同一行中调用它们时,它返回一个 无类型 错误。

我的第一个代码:

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

请您参考如下方法:

我认为您需要从 boolean indexing 中按 index 放置:

census_df = ( pd.read_csv('census.csv').drop(census_df[(census_df['SUMLEV'] == 40)].index) ) 

但更好的是使用 boolean indexing :

census_df =  pd.read_csv('census.csv') 
census_df = census_df[census_df['SUMLEV'] != 40]