Home > Blockchain >  How to insert a dataframe into another dataframe?
How to insert a dataframe into another dataframe?

Time:06-15

I have two pandas dataframe like below.
df1:

  col1 col2    col3    col4
0   45   69  string  string

df2:

   col5    col8
0    45      37
1  data  random

I want to combine these two into one dataframe.
resultant df:

col1 col2    col3    col4
  45   69  string  string
--------- 1 or 2 empty rows here -------------
col5    col8
   45      37
 data  random

I tried the append method but that puts col5 and col8 as columns. I want to combine df1 and df2. col1, col2 and other column names don't even have to be column names in the resultant dataframe. They can be values in a row. I am trying to create a summary in the resultant dataframe.

Any help is appreciated. Thank you.

CodePudding user response:

# write first dataframe
df1.to_csv('file.csv')

# append empty line
with open('file.csv', 'a') as f:
    f.write('\n\n')

# append second dataframe
df2.to_csv('file.csv', mode='a')

Output:

,col1,col2,col3,col4
0,45,69,string,string

,col5,col8
0,45,37
1,data,random

  • Related