简体   繁体   English

从 pandas 系列中的每一行中提取某个字符串的最后一次出现

[英]Extracting last occurrence of a certain string from each row in a pandas series

from each row i want to extract the last occurrence of the word "user" + the number that follow right after it from a pandas series.从每一行中,我想从 pandas 系列中提取单词“user”的最后一次出现 + 紧随其后的数字。 everything else can be discarded.其他一切都可以丢弃。 how would you perform this action?您将如何执行此操作? thanks!!!谢谢!!!

here's an example of the series:这是该系列的一个示例:

0                         1 - Unassigned, 2 - User 397335
1         1 - Unassigned, 2 - User 525767, 3 - Unassigned
2                                          1 - Unassigned
3                                          1 - Unassigned
4                                          1 - Unassigned
                               ...                       
163678                                     1 - Unassigned
163679    1 - Unassigned, 2 - User 347991, 3 - Unassigned
163680                                     1 - Unassigned
163681                                     1 - Unassigned
163682    1 - Unassigned, 2 - User 663455, 3 - Unassigned

Use str.findall :使用str.findall

>>> df['A'].str.findall(r'User \d+').str[-1]

0         User 397335
1         User 525767
2                 NaN
3                 NaN
4                 NaN
163678            NaN
163679    User 347991
163680            NaN
163681            NaN
163682    User 663455
Name: A, dtype: object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从具有 float64 数据类型的 Pandas 系列中提取某些值 - Extracting certain values from the Pandas Series with float64 datatype 熊猫:最后一次出现时分割字符串 - Pandas: Split string on last occurrence 如何在 pandas 中的数字系列中获取每一行的最后一位数字 - How to get the last digit in each row in a number series in pandas 从字符串中的某个字符中删除一行中的多次出现 - Remove multiple occurrence in a row from a certain character in a string 检查 pandas 系列中的每一行是否包含使用 apply 的列表中的字符串? - Check if each row in a pandas series contains a string from a list using apply? Pandas - 从系列中提取数据 - Pandas - Extracting data from Series 从特定列的每一行的数组中提取某些元素 - Extracting certain elements from array of each row for a specific column 在某个符号最后一次出现后切断字符串 - Cutting a string after the last occurrence of certain sign 从字符串列表中提取某些元素并使用 Pandas 转换为日期时间 - Extracting certain elements from a list of a string and turning into datetime with Pandas 从 Pandas 系列的每个值内的子字符串字典中查找子字符串的第一次出现并插入相应的字典值 - Find first occurrence of a substring from a dictionary of substrings within each value of a pandas series and insert corresponding dictionary value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM