Reputation: 29
In Python I am using an existing csv file for a project. One of it's columns is sex. So, the values are either m,f,sex, or ' '.(blank)
I only want the rows with m and f, so how do I delete the rows that have the word sex or with no value in it?
Upvotes: 1
Views: 1414
Reputation: 1572
You may read the csv file into a pandas dataFrame, then select the rows which are not blank.
import pandas as pd
inFile = "path/to/your/csv/file"
sep = ','
df = pd.read_csv(filepath_or_buffer=inFile, low_memory=False, encoding='utf-8', sep=sep)
df_mf = df.loc[(df['Sex']=='m') | (df['Sex']=='f')]
Upvotes: 2
Reputation: 48
well here's a help in pandas
import pandas as pd
df= pd.read_csv('your file path')
filt = (df['sex'] =='m') | (df['sex'] == 'f')
updated_df = df.loc[filt,['other','columns','list']]
updated_df.to_csv(r'Path where you want to store the exported CSV file\File Name.csv', index = False)
Upvotes: 1