每隔一段时间用特殊条件将空值替换为0

2024-04-26 14:37:47 发布

您现在位置:Python中文网/ 问答频道 /正文

这是数据集的以下子集:

A  B    C         D         R        sentence              ADR1         ADR2     
112 135 21  EffexorXR.21    1    lack of good feeling.     good        feeling
113 135 21  EffexorXR.21    1                               1
115 136 21  EffexorXR.21    2    Feel disconnected        disconnected   feel    
116 136 21  EffexorXR.21    2                                             0
118 142 22  EffexorXR.22    1    Weight gain               gain         
119 142 22  EffexorXR.22    1                                1             

在ADR1和ADR2列中,对于每个单词,行中应该有1或0。如果缺少值,我需要用“0”替换它。这是输出:

^{pr2}$

我试过了

df[ADR1].fillna(0, inplace=True) and df[ADR2].fillna(0, inplace=True)

但不是想要的,这是下面的代码

 A  B    C         D         R        sentence              ADR1         ADR2     
112 135 21  EffexorXR.21    1    lack of good feeling.     good        feeling
    113 135 21  EffexorXR.21    1                               1        0
    115 136 21  EffexorXR.21    2    Feel disconnected        disconnected   feel                                                                 0
    116 136 21  EffexorXR.21    2                                             0
    118 142 22  EffexorXR.22    1    Weight gain               gain           0
    119 142 22  EffexorXR.22    1                                1            0 

Tags: ofdfsentencefeelgaingoodweightinplace
2条回答

另一种方法是对每个dataframeiteratedataframerows,在第一个值不为空的情况下检查下一个值是否为空,然后将该值更新为0

col_list = ['ADR1', 'ADR2'] # columns to check
for column in col_list: # for each column go through each rows
    # however the step size is 2 at a time since current and next is checked
     for i in range(0, df.shape[0]-1, 2): 
        first_val = df.loc[i][column]
        next_val = df.loc[i+1][column]
        # check if given current not empty, is next empty
        if not first_val == '' and next_val  == '':
            df.loc[i+1, column] = 0 # update the value

您可以使用^{}来允许一次每隔一行处理数据。比如:

代码:

for col in ['ADR1', 'ADR2']:
    data = np.reshape(df[col].values, (-1, 2))
    need_fill = np.logical_and(data[:, 0] != '', data[:, 1] == '')
    data[np.where(need_fill),1] = 0

测试代码:

^{pr2}$

结果:

^{3}$

相关问题 更多 >