Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

append spliter row after rows which contains 'REL' in action column

I would like to add a split row after row which continas "REL" keyword.

import pandas as pd
data = [
    [1,'ACQ','A'],
    [2,'REL','A'],
    [3,'ACQ','B'],
    [4,'REL','B'],
    [5,'ACQ','C'],
    [6,'REL','C'],
    [7,'ACQ','A'],
    [8,'REL','A']
    ]
df = pd.DataFrame(data,columns=['x','action','name'])
print(df)

Expected output:

   x action name
0  1    ACQ    A
1  2    REL    A
2  3    ACQ    B
3  4    REL    B
4  5    ACQ    C
5  6    REL    C
6  7    ACQ    A
7  8    REL    A
      x action name
0   1.0    ACQ    A
1   2.0    REL    A
2   NaN    NaN  NaN
3   3.0    ACQ    B
4   4.0    REL    B
5   NaN    NaN  NaN
6   5.0    ACQ    C
7   6.0    REL    C
8   NaN    NaN  NaN
9   7.0    ACQ    A
10  8.0    REL    A
11  NaN    NaN  NaN

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could try with reindex

out = df.reindex(df.index.union(df.index[df.action=='REL']+0.5))
Out[22]: 
       x action name
0.0  1.0    ACQ    A
1.0  2.0    REL    A
1.5  NaN    NaN  NaN
2.0  3.0    ACQ    B
3.0  4.0    REL    B
3.5  NaN    NaN  NaN
4.0  5.0    ACQ    C
5.0  6.0    REL    C
5.5  NaN    NaN  NaN
6.0  7.0    ACQ    A
7.0  8.0    REL    A
7.5  NaN    NaN  NaN
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading