简体   繁体   English

pandas:在列包含特定值的行之后插入一行

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

I have a dataframe as follows,我有一个 dataframe 如下,

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.我想在 label 列没有值的每个位置之后添加一行,所以我做了以下操作,但是我得到了 datframe 中最后一个索引的键值错误。

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

the error is,错误是,

    raise KeyError(key) from err
KeyError: 8

my desired output is,我想要的 output 是,

    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

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:使用由助手 DataFrame 过滤的concat或由None过滤的错误值,在Series.isna中的列中设置值,然后按DataFrame.assign对索引进行DataFrame.sort_index

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM