合并列中的行并删除空白行

时间:2019-03-21 06:50:26

标签: python pandas

我正在尝试将输出显示在下面,但是我不确定如何正确地表达它。基本上,我需要将Address列合并为一行,并删除空行。以下是我的预期输出以及可重现的示例。

预期输出

    Bank           Address                Distance
0   A   Townhall Road #01-1001               10 
1   B   MapleTree Castle 10 Pathway 31       99

可复制示例

import pandas as pd 

data = {'Bank':['A', '', 'B', '', ''], 
        'Address':['Townhall Road', '#01-1001', 'MapleTree', 'Castle 10', 'Pathway 31'],
        'Distance':['10', '', '99', '', '']} 

pd.DataFrame(data) 

示例数据框

    Bank    Address   Distance
0   A   Townhall Road   10
1       #01-1001    
2   B   MapleTree       99
3       Castle 10   
4       Pathway 31  

1 个答案:

答案 0 :(得分:3)

首先用空字符串替换空值,然后向前填充缺失值。然后将GroupBy.agg与聚合joinGroupBy.first一起使用:

df['Bank'] = df['Bank'].replace('', np.nan).ffill()
df = df.groupby('Bank', as_index=False).agg({'Address':' '.join, 'Distance':'first'})
print (df)
  Bank                         Address Distance
0    A          Townhall Road #01-1001       10
1    B  MapleTree Castle 10 Pathway 31       99