df大约有200000行。一栏是电话号码。我需要用+7替换8,我不想创建另一个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中创建starts8_length11,修改它,然后将它放回df中。怎么在里面做呢?
发布于 2020-01-06 08:23:54
解决方案应该是将8替换为用于字符串开头的regex ^,但只适用于具有长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 +77787763621https://stackoverflow.com/questions/59608742
复制相似问题