简体   繁体   English

如何仅用逗号前第一个出现的单词替换 dataframe 文本列

[英]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.

相关问题 如果单元格有 2 个单词,则只提取第一个单词,如果单元格有 3 个单词,则提取第一个单词 - PANDAS/REGEX - If cell has 2 words, extract only 1st word and if cell has 3 words, extract 2 first words - PANDAS/REGEX 如何删除停用词之前出现的所有单词 - How to remove all words occuring before a stop word 如何仅用字典或文本文件中存在的单词替换 pandas dataframe 的列? - How to replace a column of a pandas dataframe with only words that exist in the dictionary or a text file? 如何在python数据框中的列中获取值的第一次出现 - How to get the 1st occurence of a value in a column in python dataframe Pandas替换DataFrame中的第一个结果 - Pandas Replace 1st Result in a DataFrame 仅替换替换第一个参数 - Replace only replacing the 1st argument fillna() 仅填充 dataframe 的第一个值 - fillna() only fills the 1st value of the dataframe 将 pandas 数据框列中的每个值与第二个数据框列的所有值相乘并将每个第一个数据框值替换为结果数组 - Multiply each value in a pandas dataframe column with all values of 2nd dataframe column & replace each 1st dataframe value with resulting array 用Python将文本文件中的单词替换为特定列中的单词 - Replace word in text file comma separated with word in a specific column with Python 用数据框中的“第 2 天”和“第 1 天”替换一列中的最后 2 个日期,以使代码动态化 - replace the last 2 dates in one column by "2nd day" and "1st day" in a dataframe to make the code dynamic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM