如何在数据框中就地替换一列中的数字?

huangapple go评论84阅读模式
英文:

How to replace digits in 1 column in dataframe inplace?

问题

df中有大约200,000行数据。其中一列是电话号码。我需要将数字8替换为+7。我不想创建另一个df。我想要原地进行替换。

df.head()

client_id    contact_number
0           +77760013505
1           +77779261433
2           +77071061047
3           +77714032401
4           87787763621
5           87787763621

我想要将那些以8开头的数字替换为+7,原地操作。

starts_8_length_11 = df[(df['contact_number'].str.startswith('8')) & (df['contact_number'].str.len() == 11)]

但我不想从主df中创建starts_8_length_11,然后对其进行更改,然后再放回df中。如何原地操作呢?

英文:

The df has some 200000 rows. One column is telephone number. I need to replace 8 with +7. I dont want to create another df. I want to do it inplace.

df.head()

client_id	contact_number	
0	        +77760013505	
1			+77779261433	
2			+77071061047	
3			+77714032401	
4			 87787763621	
5            87787763621

I want to replace those with 8 to +7 inplace.

starts_8_length_11 = df[(df['contact_number'].str.startswith('8')) & (df['contact_number'].str.len()==11)]

But I dont want to create starts8_length11 from the main df, change it then put it back into the df. How to do it inplace?

答案1

得分: 2

解决方案应该是使用正则表达式^来替换字符串开头的8,但仅针对长度为11的行:

m = (df['contact_number'].str.len()==11)

df.loc[m, 'contact_number'] = df.loc[m, 'contact_number'].str.replace('^8', '+7')
print (df)
  client_id contact_number
0         0   +77760013505
1         1   +77779261433
2         2   +77071061047
3         3   +77714032401
4         4   +77787763621
5         5   +77787763621

我认为inplace不是一个好的做法,请查看这里这里

英文:

Solution should be replace 8 with regex ^ for start of string, but only for rows with lenght 11:

m = (df['contact_number'].str.len()==11)

df.loc[m, 'contact_number'] = df.loc[m, 'contact_number'].str.replace('^8', '+7')
print (df)
  client_id contact_number
0         0   +77760013505
1         1   +77779261433
2         2   +77071061047
3         3   +77714032401
4         4   +77787763621
5         5   +77787763621

I think inplace is not good practice, check this and this.

huangapple
  • 本文由 发表于 2020年1月6日 16:18:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定