paulmassimo paulmassimo - 1 month ago 20
Python Question

Pandas set multiple line Data Frame

I use

read_csv
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
NoneType
error.

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

Answer

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]