简体   繁体   English

如何删除熊猫数据框列中数值之前的字符串?

[英]How to remove strings before a numeric value in a pandas dataframe column?

I have a pandas dataframe column with strings that looks like this: 我有一个带有如下字符串的pandas dataframe列:

Column A

text moretext 251 St. Louis Apt.54
123 Orange Drive
sometext somemoretext 171 Poplar street
textnew 11th street 
77 yorkshire avenue

I want to remove the text before the numeric values ie I want the output to be something like this: 我想删除数值之前的文本,即我希望输出是这样的:

Column A

251 St. Louis Apt.54
123 Orange Drive
171 Poplar street
11th street 
77 yorkshire avenue

Let's use regex and extract : 让我们使用正则表达式和extract

df['Column A'] = df['Column A'].str.extract(r'(\d+.+$)')

Output: 输出:

0    251 St. Louis Apt.54
1        123 Orange Drive
2       171 Poplar street
3             11th street
4     77 yorkshire avenue
Name: Column A, dtype: object

The regex states get a group of characters start with a number of any length and continue until the end of the line. 正则表达式状态使一组字符以任意长度的数字开头,并一直持续到行尾。

This function is finding the index of the first numerical character in the string and selecting the remaining part of the string. 此功能是查找字符串中第一个数字字符的索引并选择字符串的其余部分。 This function is then applied to each value of the column using apply function 然后使用apply函数将此函数应用于列的每个值

def change(string):
    for i, c in enumerate(string):
         if c.isdigit():
            idx = i
            break
    return string[idx:]

data[A] = data[A].apply(change, axis = 0)

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

相关问题 删除数值之前的字符串,但不删除如果在熊猫数据框列中包含邮政信箱地址或套件编号? - Remove strings before a numeric value but not If contains PO BOX address or Suite Number in a pandas dataframe column? 如何从熊猫数据框中的列中删除非数字字符? - How to remove non-numeric characters from a column in a pandas dataframe? 检查 pandas dataframe 中的列值是否为数字 - Check if a column value is numeric in pandas dataframe 根据条件熊猫数据框列删除字符串 - Remove strings based on condition pandas dataframe column 如何处理 pandas 中数字列上的字符串? - how to deal with strings on a numeric column in pandas? 如何根据 Pandas 数据框中的列值(int)合并行(带字符串)? - How to merge rows (with strings) based on column value (int) in Pandas dataframe? 如何在熊猫数据框中获取数字列名称 - how to get numeric column names in pandas dataframe 如何从Python中的数据框列中的字符串中删除非字母数字字符? - How to remove non-alpha-numeric characters from strings within a dataframe column in Python? Python Pandas:如何删除列值中不需要的前导字符串和试用字符串 - Python Pandas: How to Remove Unwanted Leading and Trialing Strings in Column Value 从字符串列表中删除某些字符串作为pandas.DataFrame中的列 - Remove certain strings from list of strings as column in pandas.DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM