Reputation: 1283
I am processing a large csv file. In that, for first 6 (n) rows needs to be modified.
For now, I am reading the csv in dataframe then running our process and the result I am exporting again in csv using df.to_csv()
. Wherein, df
is a dataframe object.
So, after reading the csv I'm getting the row like
0
0 COMPANY NAME XYZ LTD.
1 Region c.
2 Here some header just to make readbale for user..
0
After this, I am appending this dataframe on top of other one and then extracting into csv. But, the issue is now, that I want to add a BLANK ROW after every row in above dataframe.
Expected output for above given example
0
0 COMPANY NAME XYZ LTD.
1
2 Region c.
3
4 Here some header just to make readbale for user..
5
0
Note: The number of rows in dataframe may very so let's say we need to add blank row after every row for all n rows and then append to other df and export it to csv.
Thank you all in advance for helping me.
Upvotes: 1
Views: 201
Reputation: 23099
Most likely a multitude of ways to do this, you could simply concat both dataframes and use .loc
to assign the odd numbered index with a zero length string.
df1 = pd.concat([df,df],0).sort_index().reset_index(drop=True)
df1.loc[df1.index % 2 == 1, 0] = ''
print(df1)
0
0 COMPANY NAME XYZ LTD.
1
2 Region c.
3
4 Here some header just to make readbale for user..
5
A more simplier method would be to assign the value to your 2nd value of concat in the concat.. Note you'll need a string based column name.
#df.columns = ['A']
df1 = pd.concat([df,df.assign(A='')],0).sort_index().reset_index(drop=True)
print(df1)
A
0 COMPANY NAME XYZ LTD.
1
2 Region c.
3
4 Here some header just to make readbale for user..
5
Upvotes: 3