Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Blank Row In Python Data frame when value in column changes?

Tags:

python

pandas

I have a dataframe and I'd like to insert a blank row as a separator whenever the value in the first column changes.

For example:

Column 1     Col2    Col3    Col4
A            s       b       d
A            s       j       k
A            b       d       q
B            b       a       d
C            l       k       p

becomes:

Column 1     Col2    Col3    Col4
A            s       b       d
A            s       j       k
A            b       d       q

B            b       a       d

C            l       k       p

because the value in Column 1 changed

The only way that I figured out how to do this is using VBA as indicated by the correctly marked answer here:

How to automatically insert a blank row after a group of data

But I need to do this in Python.

Any help would be really appreciated!

like image 309
etuo Avatar asked Mar 05 '23 18:03

etuo


1 Answers

Create helper DataFrame with index values of last changes, add .5, join together with original by concat, sorting indices by sort_index, create default index by reset_index and lasr remove last row by positions with iloc:

mask = df['Column 1'].ne(df['Column 1'].shift(-1))
df1 = pd.DataFrame('',index=mask.index[mask] + .5, columns=df.columns)

df = pd.concat([df, df1]).sort_index().reset_index(drop=True).iloc[:-1]
print (df)
  Column 1 Col2 Col3 Col4
0        A    s    b    d
1        A    s    j    k
2        A    b    d    q
3                        
4        B    b    a    d
5                        
6        C    l    k    p
like image 87
jezrael Avatar answered Apr 09 '23 09:04

jezrael