I'm trying to clean some csv files from the Consumer Expenditure Survey. I want to drop any row that has a value of 0 for the Total Expenditure Column (TOTEXPCQ). What is the appropriate python code to do this using Pandas? Thanks!
Asked
Active
Viewed 52 times
0
-
1Possible duplicate of [Deleting DataFrame row in Pandas based on column value](https://stackoverflow.com/questions/18172851/deleting-dataframe-row-in-pandas-based-on-column-value) – foglerit Oct 29 '19 at 00:27
2 Answers
1
You can simply do this
df = df[df.TOTEXPCQ!= 0]
OR
df.drop(df.loc[df['TOTEXPCQ']==0].index, inplace=True)

tyb9900
- 214
- 2
- 11
0
import pandas as pd
data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
'year': [2012, 2012, 2013, 2014, 2014],
'TOTEXPCQ': [4, 0, 31, 2, 0]}
df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])
df
Then do this:
df[df['TOTEXPCQ']!=0]

Bill Chen
- 1,699
- 14
- 24