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

pandas: insert a row after a row where the column contains a specific value

I have a dataframe as follows,

import pandas as pd
import numpy as np
df= pd.DataFrame({"text['open','the','door','val','close','the','door','val'],"label":['O','B','D',None,'C','E','N',None]})

I would like to add a row after every where the column label has a none value, so I did the following, but I get a key value error for the last index in the datframe.

df2= np.where(df.label== None, df.loc[len(df)]==['new_val','new_val'], df)
print(df2)

the error is,

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

    raise KeyError(key) from err
KeyError: 8

my desired output is,

    text label
0   open     O
1    the     B
2   door     D
3    val  None
4   new_val new_val
5  close     C
6    the     E
7   door     N
8    val  None
9   new_val new_val

>Solution :

Use concat by helper DataFrame filtered by None or misisng values by Series.isna, set values in columns in DataFrame.assign and then sort index by DataFrame.sort_index with created default indices:

df = (pd.concat([df, df[df.label.isna()].assign(text='new_val',label='new_val')])
        .sort_index()
        .reset_index(drop=True))
print (df)
      text    label
0     open        O
1      the        B
2     door        D
3      val     None
4  new_val  new_val
5    close        C
6      the        E
7     door        N
8      val     None
9  new_val  new_val
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