3 回答

TA贡献1796条经验 获得超7个赞
使用 s.str.extract
df['Currency']=df.Salary.str.extract('(\W)')
#or df['Currency']=df.Salary.str.split("_").str[1]
\W --> 任何不是字母、数字或下划线字符的字符。
print(df)
Test1 Test2 Salary Currency
0 1 Akash 100_$ $
1 2 Akash 200_@ @
2 3 Yogi 300_% %
3 4 Akash 400_$ $

TA贡献1801条经验 获得超8个赞
不使用str.split尝试以下。
df['Currency']=df.Salary.str.replace(".*_", "")
df
Test1 Test2 Salary Currency
0 1 Akash 100_$ $
1 2 Akash 200_@ @
2 3 Yogi 300_% %
3 4 Akash 400_$ $
或(如果有人想使用str.split:
您能否尝试以下操作,在此处使用str.split。
df['Currency']=df.Salary.str.split('_').str[1]
打印df输出如下。
Test1 Test2 Salary Currency
0 1 Akash 100_$ $
1 2 Akash 200_@ @
2 3 Yogi 300_% %
3 4 Akash 400_$ $
添加回答
举报