熊猫:使用另一列作为来源替换值

时间:2020-05-25 12:30:18

标签: python pandas

我有一个这样的数据框:

Source    | Description  |
Incomes   | Tax 12       |
Incomes   | Payment      |
Incomes   | Check 152    |
Incomes   | Incoming 21  |
Incomes   | Receiving    |
Payments  | Tax          |
Payments  | Incoming 7   |
Payments  | Receiving 12 |
Payments  | Check        |
Payments  | Incoming     |

首先,我使用Incoming将包含CheckIncomes的所有内容替换为Checkdf.loc

Source    | Description  |
Incomes   | Incomes      |
Incomes   | Payment      |
Incomes   | Checks       |
Incomes   | Incomes      |
Incomes   | Receiving    |
Payments  | Receiving 2  |
Payments  | Incomes      |
Payments  | Receiving 12 |
Payments  | Checks       |
Payments  | Incomes      |

现在,我想用相应的Incomes值替换Checks列中与DesciptionSource不匹配的所有内容,例如:

Source    | Description  |
Incomes   | Incomes      |
Incomes   | Incomes      |
Incomes   | Checks       |
Incomes   | Incomes      |
Incomes   | Incomes      |
Payments  | Payments     |
Payments  | Incomes      |
Payments  | Payments     |
Payments  | Checks       |
Payments  | Incomes      |

我该怎么做?

我已经尝试过df.loc[df['Description'].str.contains('Incomes|Checks')== False] == df['Source'],但没有成功。

预先感谢

1 个答案:

答案 0 :(得分:0)

使用此

mask = df['Description'].str.contains("Incoming|Check")

df.loc[~mask, "Description"] = df.loc[~mask, "Source"]

df['Description'] = df['Description'].str.replace("Incoming.*", "Incomes") \
    .str.replace("Check.*", "Checks")