0

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 = pd.Series(0, df.columns)
f = lambda d: d.append(s, ignore_index=True)
grp = np.arange(len(df)) // 2
df= df.groupby(grp, group_keys=False).apply(f).reset_index(drop=True)

And I am printing my final dataframe using this

print(final.to_string(index=False))

But when I tried the above code I got the below error

ValueError: Grouper and axis must be same length

My data frame has composite numbered rows and 5 columns. Could you please help?

Also I need to get the code converted to console application(.exe)

bb1
  • 7,174
  • 2
  • 8
  • 23

2 Answers2

0

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)) // 3
print(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               
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
0

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)
def f(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))