在python中,我想遍历多个csv文件并删除特定的行



我有10个csv文件,在每个文件中,我想删除UID列中包含以下数字的行——100210071008

请注意,所有10个csv文件都有相同的列名

# one of the csv files looks like this
import pandas as pd
df = { 
'UID':[1001,1002,1003,1004,1005,1006,1007,1008,1009,1010],
'Name':['Ray','James','Juelz','Cam','Jim','Jones','Bleek','Shawn','Beanie','Amil'],
'Income':[100.22,199.10, 191.13,199.99,230.6,124.2,122.9,128.7,188.12,111.3],
'Age':[24,32,27,54,23,41,44,29,30,68]
}

df = pd.DataFrame(df)
df = df[['UID','Name','Age','Income']]
df 

尝试

#I know I need a for loop or glob to iterate through the folder and filter out the desired UIDs. My dilemma is I don't know how to incorporate steps II & III  in I
#Step I: looping through the .csv files in the folder
import os
directory = r'C:Usersadmin'
for filename in os.listdir(directory):
if filename.endswith(".csv"):
print(os.path.join(directory, filename))
# StepII: UID to be removed - 1002,1007,1008 
df2 = df[~(df.UID.isin([1002,1007,1008]))] 
# Step III: Export the new dataframes as .csv files (10 csv files)
df2.to_csv(r'mypathdata.csv)

感谢

试试这个:

import os
directory = r'C:Usersadmin'
for filename in os.listdir(directory):
if filename.endswith(".csv"):
filepath = os.path.join(directory, filename)
df = pd.read_csv(filepath)
df2 = df[~df['UID'].isin([1002,1007,1008])]
filename, ext = filepath.rsplit('.', maxsplit=1)
filename = f'{filename}_mod.{ext}'
df2.to_csv(filename)

注:@TimRoberts是对的,熊猫在这里被高估了,但如果你想在这里学习,有一个潜在的解决方案。

你不需要一个程序,当然也不需要panda。如果你有Linux工具:

grep -v -e 1002, -e 1007, -e 1008, incoming.csv > fixed.csv

Windows:

findstr /v /c:1002, /c:1007, /c:1008, incoming.csv > fixed.csv

因此,在批处理文件中:

cd C:Usersadmin
mkdir fixed
for %i in (*.csv) do findstr /v /c:1002, /c:1007, /c:1008, %%i > fixed%%i

很抱歉我的英语不好

第二步:

如果我还没有完全理解,那么您希望从df dictionary中的这个列表[1010011002100310041005100610071008810091010]中删除值[1000210071008]。很简单,你可以像这样迭代dict的键:

values = [1002,1007,1008] 
for key in df.keys():

然后检查该密钥的值中是否有要删除的值

values = [1002,1007,1008] 
for key in df.keys():
for value in values:
if value in df[key]:
df[key].remove(value)

步骤iii

import csv
with open('my_file.csv', mode='w') as file:
file_writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
file_writer.writerow(df)

相关内容

  • 没有找到相关文章

最新更新