I am new to Python and am looking for a simple solution.
I have several .csv files with the same structure (number of columns and lines) in one folder. The path is: C:\temp
Now I want to read all these .csv files into a new dataframe, which I want to export later as a new .csv file.
up to now i have read each .csv file by hand and saved it into a pandas dataframe.
Here is an example:
df1 = pd.read_csv(r "C:\temp\df1.csv", header= None)
df2 = pd.read_csv(r "C:\temp\df2.csv", header= None)
df1
0 id Feature
1 1 12
2 2 13
3 3 14
4 4 15
5 5 16
6 7 17
7 8 15
8 9 12
9 10 13
10 11 23
Then I used .append to merge the dataframes.
df_new = df1.append(df2)
0 id Feature
1 1 12
2 2 13
3 3 14
4 4 15
5 5 16
6 7 17
7 8 15
8 9 12
9 10 13
10 11 23
0 id Feature
1 1 14
2 2 9
3 3 3
4 4 8
5 5 9
6 7 1
7 8 32
8 9 7
9 10 3
10 11 12
df_new.to_csv('df_new.csv', index=False)
Unfortunately this version always has the header with me, but I don't need it. So I deleted it afterwards by hand.
Isn't there a faster version? I'm thinking of a for loop that opens all existing .csv files in the path and reads them line by line into a new dataframe and at the end of the loop makes a .csv file out of it? Unfortunately I have no experience with loops.
I appreciate your help.