adding dataframes to same csv with blank row in between

Question:

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.

Asked By: Giulia

||

Answers:

Use pandas.DataFrame with np.nan to create the empty row :

import numpy as np

empty_df = pd.DataFrame([[np.nan] * len(df1.columns)], columns=df1.columns)

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

df2.to_csv('foo.csv', mode='a', index=False)
# Output (in Excel):

enter image description here

Answered By: abokey
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.