如果值是熊猫的Nan则合并两行

时间:2020-01-08 12:36:43

标签: python python-3.x pandas data-science

我有下面的表格

                   Error Code ID      Error Code              Description of  Error Code
0                                NaN      Value                                     NaN
1                     OE_ATO_IN_OPEN      16169      Order priced ATO cannot be entered
2                                NaN        NaN                when a security is open.
3                      e$dup_request      16198  Duplicate modification or cancellation
4                                NaN        NaN     request for the same trade has been
5                                NaN        NaN                            encountered.
6          TRD_CONT_MOD_NOT_ALLOWED      16231     Continuous session trades 
7       STR_PRO_PARTIVIPANT_INVALID      16233     Proprietary requests cannot be made

我想将其转换为

                   Error Code ID     Error Code Value        Description of  Error Code
1                     OE_ATO_IN_OPEN      16169      Order priced ATO cannot be entered               
                                                               when a security is open.
3                      e$dup_request      16198  Duplicate modification or cancellation
                                                    request for the same trade has been
                                                                           encountered.
6          TRD_CONT_MOD_NOT_ALLOWED      16231     Continuous session trades 
7       STR_PRO_PARTIVIPANT_INVALID      16233     Proprietary requests cannot be made

缺少索引只是为了理解。

1 个答案:

答案 0 :(得分:1)

首先用ffill向前填充缺失值,然后聚合join

c = ['Error Code ID', 'Error Code']
df[c] = df[c].ffill()
df = df.groupby(c).agg(' '.join).reset_index()

如果要删除Description of Error Code列中缺少值的行,请使用:

df = df.dropna(subset=['Description of  Error Code'])
c = ['Error Code ID', 'Error Code']
df[c] = df[c].ffill()
df = df.groupby(c).agg(' '.join).reset_index()

print (df)
                 Error Code ID Error Code  \
0               OE_ATO_IN_OPEN      16169   
1  STR_PRO_PARTIVIPANT_INVALID      16233   
2     TRD_CONT_MOD_NOT_ALLOWED      16231   
3                e$dup_request      16198   

                           Description of Error Code  
0  Order priced ATO cannot be entered when a secu...  
1                Proprietary requests cannot be made  
2                          Continuous session trades  
3  Duplicate modification or cancellation request...