从多行熊猫中合并文本

问题描述 投票:0回答:2

我只希望在满足某些特定条件的情况下合并相应行数据的内容。这是我正在处理的test数据帧

    Date        Desc    Debit   Credit  Bal
0   04-08-2019  abcdef  45654   NaN     345.0
1   NaN         jklmn   NaN     NaN     6
2   04-08-2019  pqr     NaN     23      368.06
3   05-08-2019  abd     23      NaN     345.06
4   06-08-2019  xyz     NaN     350.0   695.06

其中,我要将nan所在的行连接到Date中的上一行。需要输出:

    Date        Desc        Debit   Credit  Bal
0   04-08-2019  abcdefjklmn 45654   NaN     345.06
1   NaN         jklmn       NaN     NaN     6
2   04-08-2019  pqr         NaN     23      368.06
3   05-08-2019  abd         23      NaN     345.0
4   06-08-2019  xyz         NaN     350.0   695.06

如果有人帮我解决这个问题?我尝试了以下方法:

for j in [x for x in range(lst[0], lst[-1]+1) if x not in lst]:
    print (test.loc[j-1:j, ].apply(lambda x: ''.join(str(x)), axis=1))

但无法获得预期的结果。

python-3.x pandas merge concatenation rows
2个回答
0
投票

您可以使用

d = df["Date"].fillna(method='ffill')
df.update(df.groupby(d).transform('sum'))
print(df)

输出

          Date  Desc              Debit     Credit  Bal
0   04-08-2019  abcdefjklmn     45654.0     0.0     351.0
1   NaN         abcdefjklmn     45654.0     0.0     351.0
2   05-08-2019  abd                45.0     0.0     345.0
3   06-08-2019  xyz                 0.0     345.0   54645.0

0
投票
idx = test.loc[test["Date"].isna()].index
test.loc[idx-1, "Desc"] = test.loc[idx-1]["Desc"].str.cat(test.loc[idx]["Desc"])
test.loc[idx-1, "Bal"] = (test.loc[idx-1]["Bal"].astype(str)
                            .str.cat(test.loc[idx]["Bal"].astype(str)))

## I tried to add two values but it didn't work as expected, giving 351.0
# test.loc[idx-1, "Bal"] = test.loc[idx-1]["Bal"].values + test.loc[idx]["Bal"].values

         Date         Desc    Debit  Credit       Bal
0  04-08-2019  abcdefjklmn  45654.0     NaN  345.06.0
1         NaN        jklmn      NaN     NaN         6
2  05-08-2019          abd     45.0     NaN       345
3  06-08-2019          xyz      NaN   345.0     54645
© www.soinside.com 2019 - 2024. All rights reserved.