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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With