1

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.

1 Answer 1

3

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                                                   
     
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Manakin... that works.. and I'm exactly looking something like this. Thanks for your help... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.