[英]Pandas - Drop rows based on row value for multiple csv files
There are multiple csv files in which I want to delete entire rows based on some specific keywords contained in rows.有多个 csv 文件,我想根据行中包含的某些特定关键字删除整行。 If the csv files contain
x
, p
or z
in any row delete the entire row.如果 csv 文件的任何行中包含
x
、 p
或z
,则删除整行。 It will loop through all csv files.它将遍历所有 csv 个文件。 Sample dataset:
示例数据集:
>> df
k y j
0 a NaN x
1 p v z
2 m n 9
Expected outcome:预期结果:
k y j
0 m n 9
I tried:我试过了:
import os
import glob
import pandas as pd
os.chdir(r'mypath')
allFiles = glob.glob("*.csv")
for file in allFiles:
df = pd.read_csv(file)
#then no idea what to do
Try:尝试:
df = df[(df.ne("x")&df.ne("p")&df.ne("z")).all(axis=1)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.