'Save multiple dataframes to the same file, one after the other
Lets say I have three dfs
x,y,z
0,1,1,1
1,2,2,2
2,3,3,3
a,b,c
0,4,4,4
1,5,5,5
2,6,6,6
d,e,f
0,7,7,7
1,8,8,8
2,9,9,9
How can I stick them all together so that i get:
x,y,z
0,1,1,1
1,2,2,2
2,3,3,3
a,b,c
0,4,4,4
1,5,5,5
2,6,6,6
d,e,f
0,7,7,7
1,8,8,8
2,9,9,9
I am not fussed if it's in a df or not hence I haven't included a new index. Essentially I just want to glue n amount of dfs together to save me having to copy and paste the data into an excel sheet myself.
Solution 1:[1]
If you want to save all your dataframes in the same file one after the other, use a simple loop with to_csv
and use the file append mode (a):
dfs = [df1, df2, df3]
for d in dfs:
d.to_csv('out.csv', mode='a')
NB. the initial file must be empty or non existent
output out.csv
:
,x,y,z
0,1,1,1
1,2,2,2
2,3,3,3
,a,b,c
0,4,4,4
1,5,5,5
2,6,6,6
,d,e,f
0,7,7,7
1,8,8,8
2,9,9,9
Solution 2:[2]
Have a look at the to_csv()
method of DataFrame.
print(df1.to_csv(index=False))
print(df2.to_csv(index=False))
print(df3.to_csv(index=False))
That being said, there is a to_excel()
method as well that may solve the potential XY Problem referenced in the comments.
Solution 3:[3]
You might try this:
start_df = df1
other_dfs = [df2, df3]
new_df = pd.concat([start_df, *[x[::-1].append(dict(zip(x.columns, x.columns)), ignore_index=True)[::-1].reset_index(drop=True).set_axis(start_df.columns, axis=1) for x in other_dfs]], ignore_index=True)
Output:
>>> new_df
x y z
0 1 1 1
1 2 2 2
2 3 3 3
3 a b c
4 4 4 4
5 5 5 5
6 6 6 6
7 d e f
8 7 7 7
9 8 8 8
10 9 9 9
Basically what we're doing here is, for each df in other_dfs
, we reverse it, append the columns of that dataframe to the end, re-reverse it, change its columns to match start_df
's columns, and then we concatenate them all together.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sunderam Dubey |
Solution 2 | JNevill |
Solution 3 | Sunderam Dubey |