[英]How to replace dataframe text column with only the 1st occuring word / words before a comma
The dataframe for the problem statement looks like问题陈述的 dataframe 看起来像
Name姓名 | UID用户标识符 | search_text搜索文本 |
---|---|---|
B乙 | 14 14 | kj kj |
S小号 | 2 2 | hsa,isd hsa,isd |
D D | 10 10 | sa,ad,ad萨,广告,广告 |
E乙 | 99 99 | pid, pd,dd,ef PID、PD、DD、EF |
G G | 8 8 | dd dd |
I want the dataframe search_text to be stripped and replaced on the 1st word before comma.(I dont want to manually map it and replace).我希望 dataframe search_text 在逗号前的第一个单词上被剥离和替换。(我不想手动 map 并替换它)。 So it would look like.所以它看起来像。
Name姓名 | UID用户标识符 | search_text搜索文本 |
---|---|---|
B乙 | 14 14 | kj kj |
S小号 | 2 2 | hsa高铁 |
D D | 10 10 | sa萨 |
E乙 | 99 99 | pid PID |
G G | 8 8 | dd dd |
Is there any convenient way to do that?有什么方便的方法吗?
Extract the first alphanumerics in the string提取字符串中的第一个字母数字
df['search_text'] = df['search_text'].str.extract('(^\w+)')
Name UID search_text
0 B 14 kj
1 S 2 hsa
2 D 10 sa
3 E 99 pid
4 G 8 dd
Use Series.str.split
使用Series.str.split
df['search_text'] = df['search_text'].str.split(',').str[0]
print(df)
Name UID search_text
0 B 14 kj
1 S 2 hsa
2 D 10 sa
3 E 99 pid
4 G 8 dd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.