在Python中,我正在为一个项目使用现有的csv文件。其中一个栏目是性爱。因此,值为m、f、性别或‘’。(空白)
我只想要包含m和f的行,那么如何删除其中包含单词性别或没有值的行呢?
发布于 2020-05-03 01:25:32
您可以将csv文件读取到pandas dataFrame中,然后选择不为空的行。
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')]发布于 2020-05-03 01:25:10
好了,这是对熊猫的帮助
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)https://stackoverflow.com/questions/61563038
复制相似问题