paulmassimo paulmassimo - 6 months ago 85
Python Question

Pandas set multiple line Data Frame

I use

and want to drop some rows that have specific value, I could do it good in separate lines, but when I want to invoke both in same line, it returns a

My first code:

census_df = pd.read_csv('census.csv')
census_df.drop(census_df['SUMLEV'] == 40, inplace=True)

And I want to do:

census_df = (pd.read_csv('census.csv')
.drop(census_df['SUMLEV'] == 40, inplace=True))

It returned an error:

TypeError: 'NoneType' object is not subscriptable


I think you need drop by index from boolean indexing:

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

But better is use boolean indexing:

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