简体   繁体   English

合并 pandas 中多行的文本

[英]Combine text from multiple rows in pandas

I want to merge content for respective rows' data only where some specific conditions are met.我只想在满足某些特定条件的情况下合并各行数据的内容。 Here is the test dataframe I am working on这是我正在做的test dataframe

    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

in which, I want to join the rows where there is nan into Date to the previous row.其中,我想将存在nan的行加入Date到上一行。 Output required:需要 Output:

    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

If anybody help me out with this?如果有人帮我解决这个问题? I have tried the following:我尝试了以下方法:

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))

But could not get the expected result.但无法得到预期的结果。

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

You can use您可以使用

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

output output

          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

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

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