I'm really new to python, so please bear with me!
I have folder on my desktop that contains a few csv files named as "File 1.csv", "File 2.csv" and so on. In each file, there is a table that looks like:
Animal Level
Cat 1
Dog 2
Bird 3
Snake 4
But each one of the files has a few differences in the "Animal" column. I wrote the following code that compares only two files at a time and returns the animals that match:
def matchlist(file1, file2):
new_df = pd.DataFrame()
file_one = pd.read_csv(file1)
file_two = pd.read_csv(file2)
for i in file_one["Animal"]:
df_temp = file_two[file_two["Animal"] == i]
new_df = new_df.append(df_temp)
df_temp = pd.DataFrame()
return new_df
But that only compares two files at a time. Is there a way that will iterate through all the files in that single folder and return all the ones that match to the new_df above?
For example, new_df compares file 1 and file 2. Then, I am looking for code that compares new_df to file 3, file 4, file 5, and so on.
Thanks!