英文:
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论