I have 10 csv files, in each file, I want to remove rows containing the following numbers in the UID column - 1002, 1007,1008.
Please note, all 10 csv files have the same column names
# 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
Attempt
#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:\Users\admin'
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'mypath\data.csv)
Thanks