简体   繁体   English

python Pandas替换字符串中的单词

[英]python Pandas replace the word in the string

Given a dataframe like: 给定一个数据帧,如:

A    B    C
1    a    yes
2    b    yes
3    a    no

I would like to change the dataframe to: 我想将数据框更改为:

A    B    C
1    a    yes
2    b    no
3    a    no

which means that if column B has the value 'b', I want to change the column C to 'no'. 这意味着如果列B的值为'b',我想将列C更改为'no'。 Which can be represented by df[df['B']=='b']['C'].str.replace('yes','no') . 这可以用df[df['B']=='b']['C'].str.replace('yes','no') But use this will not change dataframe df itself. 但是使用它不会改变数据帧df本身。 Even I tried df[df['B']=='b']['C'] = df[df['B']=='b']['C'].str.replace('yes','no') it didn't work. 甚至我试过df[df['B']=='b']['C'] = df[df['B']=='b']['C'].str.replace('yes','no')它不起作用。 I am wondering how to solve this problem. 我想知道如何解决这个问题。

Solutions with set values by mask : 通过mask设置值的解决方案:

df.loc[df.B == 'b', 'C'] = 'no'
print (df)
   A  B    C
0  1  a  yes
1  2  b   no
2  3  a   no

df['C'] = df['C'].mask(df.B == 'b','no')
print (df)
   A  B    C
0  1  a  yes
1  2  b   no
2  3  a   no

Solutions with replace only yes string: 解决方案只替换yes字符串:

df.loc[df.B == 'b', 'C'] = df['C'].replace('yes', 'no')
print (df)
   A  B    C
0  1  a  yes
1  2  b   no
2  3  a   no

df['C'] = df['C'].mask(df.B == 'b', df['C'].replace('yes', 'no'))
print (df)
   A  B    C
0  1  a  yes
1  2  b   no
2  3  a   no

Difference better seen in changed df : 在改变的df看到的差异更好:

print (df)
   A  B        C
0  1  a      yes
1  2  b      yes
2  3  b  another
3  4  a       no

df['C_set'] = df['C'].mask(df.B == 'b','no')
df['C_replace'] = df['C'].mask(df.B == 'b', df['C'].replace('yes', 'no'))

print (df)
   A  B        C C_set C_replace
0  1  a      yes   yes       yes
1  2  b      yes    no        no
2  3  b  another    no   another
3  4  a       no    no        no

EDIT: 编辑:

In your solution is necessary only add loc : 在您的解决方案中只需添加loc

df.loc[df['B']=='b', 'C'] = df.loc[df['B']=='b', 'C'].str.replace('yes','no')
print (df)
   A  B        C
0  1  a      yes
1  2  b       no
2  3  b  another
3  4  a       no

EDIT1: EDIT1:

I was really curious what method is fastest: 我真的很好奇什么方法最快:

#[40000 rows x 3 columns]
df = pd.concat([df]*10000).reset_index(drop=True)    
print (df)

In [37]: %timeit df.loc[df['B']=='b', 'C'] = df['C'].str.replace('yes','no')
10 loops, best of 3: 79.5 ms per loop

In [38]: %timeit df.loc[df['B']=='b', 'C'] = df.loc[df['B']=='b','C'].str.replace('yes','no')
10 loops, best of 3: 48.4 ms per loop

In [39]: %timeit df.loc[df['B']=='b', 'C'] = df.loc[df['B']=='b', 'C'].replace('yes','no')
100 loops, best of 3: 14.1 ms per loop

In [40]: %timeit df['C'] = df['C'].mask(df.B == 'b', df['C'].replace('yes', 'no'))
100 loops, best of 3: 10.1 ms per loop

# piRSquared solution with replace
In [53]: %timeit df.C = np.where(df.B.values == 'b', df.C.replace('yes', 'no'), df.C.values)
100 loops, best of 3: 4.74 ms per loop

EDIT1: EDIT1:

Better is change condition - add df.C == 'yes' or df.C.values == 'yes' if need fastest solution: 更好的是更改条件 - 如果需要最快的解决方案,请添加df.C == 'yes'df.C.values == 'yes'

df.loc[(df.B == 'b') & (df.C == 'yes'), 'C'] = 'no'

df.C = np.where((df.B.values == 'b') & (df.C.values == 'yes'), 'no', df.C.values)

np.where

df.C = np.where(df.B == 'b', 'no', df.C)

Or 要么

df.C = np.where(df.B.values == 'b', 'no', df.C.values)

pd.Series.mask

df.C = df.C.mask(df.B == 'b', 'no')

All change df in place and yield 所有改变df到位和产量

   A  B    C
0  1  a  yes
1  2  b   no
2  3  a   no

timing 定时 在此输入图像描述

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

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