Home > Back-end >  adding dataframes to same csv with blank row in between
adding dataframes to same csv with blank row in between

Time:09-23

I have two dataframes:

In [31]: df1
Out[31]:
        State  Score
0  Arizona AZ     62
1  Georgia GG     47
2  Newyork NY     55
3  Indiana IN     74
4  Florida FL     31

and

In [30]: df3
Out[30]:
  letter  number animal
0      c       3    cat
1      d       4    dog

I want to obtain a csv like this:

1       State  Score
2  Arizona AZ     62
3  Georgia GG     47
4  Newyork NY     55
5  Indiana IN     74
6  Florida FL     31
7
8 letter  number animal
9      c       3    cat
8      d       4    dog

I was able to obtain it by creating an empty dataframe, appending it to the first dataframe and then adding the second dataframe to the csv like this:

empty_df = pd.Series([],dtype=pd.StringDtype())
df1.append(empty_df, ignore_index=True).to_csv('foo.csv', index=False)
df3.to_csv('foo.csv', mode='a', index=False)

but I am getting a warning that the function 'append' is getting deprecated and I should be using 'concat'.

I tried this with concat:

pd.concat([df1, empty_df], ignore_index=True).to_csv('foo.csv', index=False)
df3.to_csv('foo.csv', mode='a', index=False)

but I am not getting the empty line between the 2 sets of data.

CodePudding user response:

Use enter image description here

  • Related