大熊猫数据框中的条件替换

时间:2020-11-02 16:10:44

标签: python pandas dataframe

我有以下数据框:

df:

A   B                       C
121 The price is $4M        USA
323 The price is $2.2M      USA
454 The price is $62K       Japan
654 The price is $91M(-21%) Japan
877 The price is $432M(91%) USA

我正在尝试根据C列中的值替换B列。

预期的数据框:

df:

A   B                       C
121 The price is $4M        USA
323 The price is $2.2M      USA
454 The price is Y62K       Japan
654 The price is Y91M(-21%) Japan
877 The price is $432M(91%) USA

我该怎么做?

我尝试了以下方法,但是它不起作用:

df[(df['C']=='Japan')]['B'].replace(r'\$', "Y")

df[(df['C']=='Japan')]['B'].replace({'$':'Y'}, regex=True)

3 个答案:

答案 0 :(得分:2)

您可以使用mask

df["B"] = df["B"].mask(df["C"].eq("Japan"), df["B"].str.replace("$", "Y"))

print (df)

     A                        B      C
0  121         The price is $4M    USA
1  323       The price is $2.2M    USA
2  454        The price is Y62K  Japan
3  654  The price is Y91M(-21%)  Japan
4  877  The price is $432M(91%)    USA

答案 1 :(得分:1)

使用Assign函数执行您现在正在做的事情:

df = df.assign(
    B = lambda x: x.apply(lambda s: x['B'].replace('$', "Y") if x['C'] =='Japan')
)

答案 2 :(得分:1)

这是另一种方式。

df.loc[df['C'] == 'Japan','B'] = df['B'].str.replace('$','Y')