我想从Name
中提取一个子字符串,并在Address
的前面添加,但是cat
默认情况下将其添加为到底。
我的数据:
Name | Address
Eleanor A. Martin #/222 Rhapsody | Street 32601 Florida
Ann K. Wagner | 3071 Half and Half Drive Hialeah FL 33012
我的代码:
df = pd.DataFrame([['Eleanor A. Martin #/222 Rhapsody ','Street 32601 Florida'],['Ann K. Wagner','3071 Half and Half Drive Hialeah FL 33012']],columns=['Name','Address'])
df['Address'] = df['Address'].str.cat(df['Name'].str.extract(r'#/(.*)'), sep=' ', na_rep = '').str.strip()
当前结果:
Name | Address
Eleanor A. Martin #/222 Rhapsody | Street 32601 Florida 222 Rhapsody
Ann K. Wagner | 3071 Half and Half Drive Hialeah FL 33012
所需结果:
Name | Address
Eleanor A. Martin #/222 Rhapsody | 222 Rhapsody Street 32601 Florida
Ann K. Wagner | 3071 Half and Half Drive Hialeah FL 33012
这不适用于我的集合(显示不同的行)
df['Address'] = df['Name'].str.extract(r'#/(.*)') + " " + df['Address']
如何在Name
中的字符串前面添加Address
中的子字符串?
答案 0 :(得分:1)
首先为Series.str.extract
中的expand=False
添加参数Series
,添加分隔符并将丢失的值替换为空字符串,最后添加第二列:
df['Address'] = (df['Name'].str.extract(r'#/(.*)', expand=False).add(" ").fillna('') +
df['Address'])
替代:
df['Address'] = ((df['Name'].str.extract(r'#/(.*)', expand=False) + " ").fillna('') +
df['Address'])
print (df)
Name \
0 Eleanor A. Martin #/222 Rhapsody
1 Ann K. Wagner
Address
0 222 Rhapsody Street 32601 Florida
1 3071 Half and Half Drive Hialeah FL 33012
答案 1 :(得分:-1)
类似于您的原始解决方案:
df['Address'] = df['Name'].str.extract(r'#/(.*)').str.cat(df['Address'], sep=' ', na_rep = '').str.strip()