Skip to content Skip to sidebar Skip to footer

Adding A Row Of Special Character After Every Nth Row (n Variable But Pre-decided) In Pandas Dataframe

I am trying to insert a row of * (any special character would do) after every n rows of my pandas data-frame. One of the SO post says it can be done using the following code: s = p

Solution 1:

Looks like you are trying to just add a line. I used the same code you have and it works. Not sure why it is not working for you.

import pandas as pd
import numpy as np
df = pd.DataFrame({'col1':['hello','hi','Hola','Bye','Ta','Goodbye','xyz','abc','def'],
                   'col2':[200,300,400,500,600,700,800,900,1000]})
print (df)

s = pd.Series('', df.columns)
f = lambda d: d.append(s, ignore_index=True)
grp = np.arange(len(df)) // 3print(df.groupby(grp, group_keys=False).apply(f).reset_index(drop=True))

Here's the output:

Before:

      col1  col2
0    hello   200
1       hi   300
2     Hola   400
3      Bye   500
4       Ta   600
5  Goodbye   700
6      xyz   800
7      abc   900
8      def  1000

After:

       col1  col2
0     hello   200
1        hi   300
2      Hola   400
3                
4       Bye   500
5        Ta   600
6   Goodbye   700
7                
8       xyz   800
9       abc   900
10      def  1000
11               

I split it after every 4 rows with the same code (instead of //3 I gave //4).

The output is:

       col1  col2
0     hello   200
1        hi   300
2      Hola   400
3       Bye   500
4                
5        Ta   600
6   Goodbye   700
7       xyz   800
8       abc   900
9                
10      def  1000
11               

Solution 2:

For anyone who faces the same issue in future, the final code snippet which worked for me is reproduced below. @Joe Ferndz, thanks a lot buddy.

num = some_user_input.count('+') + 1# this will be used later

s = pd.Series(' ', df.columns)
deff(d): return d.append(s, ignore_index=True)
grp = np.arange(len(df)) // num
df_spaced = df.groupby(grp, group_keys=False).apply(f).reset_index(drop=True)
print(df_spaced.to_string(index=False))

Post a Comment for "Adding A Row Of Special Character After Every Nth Row (n Variable But Pre-decided) In Pandas Dataframe"