简体   繁体   English

如何从 pandas 字符串列中提取每个数字以列出?

[英]how to extract each numbers from pandas string column to list?

How to do that?怎么做?

I have pandas dataframe looks like:我有 pandas dataframe 看起来像:

Column_A
11.2 some text 17 some text 21
some text 25.2 4.1 some text 53 17 78
121.1 bla bla bla 14 some text
12 some text

I need to transfer this each row to separated list:我需要将每一行转移到单独的列表中:

listA[0] = 11.2 listA[1] = 17 listA[2] = 21
listB[0] = 25.2 listB[1] = 4.1 listB[2] = 53 listB[3] = 17 listB[4] = 78
listC[0] = 121.1 listC[1] = 14
listD[0] = 12

You can use re to find all the occurrences of the numbers either integer or float.您可以使用re查找所有出现的数字 integer 或浮点数。

df['Column_A'].apply(lambda x: re.findall(r"[-+]?\d*\.\d+|\d+", x)).tolist()

OUTPUT : OUTPUT

[['11.2', '17', '21'], ['25.2', '4.1', '53', '17', '78'], ['121.1', '14'], ['12']]

If you want, you can type cast them to float / int checking if the extracted string has .如果需要,您可以将它们类型转换为float / int检查提取的字符串是否具有. in them, something like this:在他们中,是这样的:

df['Column_A'].apply(lambda x: re.findall(r"[-+]?\d*\.\d+|\d+", x)).map(lambda x: [int(i) if '.' not in i else float(i) for i in x]).tolist()

OUTPUT : OUTPUT

[[11.2, 17, 21], [25.2, 4.1, 53, 17, 78], [121.1, 14], [12]]

As pointed by @Uts, we can directly call findall over Series.str as:正如@Uts 所指出的,我们可以直接通过Series.str调用findall

listA, listB, listC, listD = df.Column_A.str.findall(r"[-+]?\d*\.\d+|\d+")
[re.findall(r"\d+?\.?\d*", x) for x in Column_A]

You'll get a list of lists for every row in that column.您将获得该列中每一行的列表列表。 It will be strings but you can convert to float however you want after that.它将是字符串,但之后您可以根据需要转换为浮点数。

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

相关问题 从熊猫数据框中的列中提取字符串中的数字 - Extract the numbers in a string from a column in pandas dataframe 如何从列表字符串的列中提取值 pandas dataframe - How to extract values from a column of list string pandas dataframe 从 pandas 的列表列中,访问列表中的每个字符串以删除数字和句点 - From list column in pandas, access each string in list to remove the numbers and periods 从 Pandas DF 的字符串列中提取数字 - Extract numbers from string column from Pandas DF 如何从 Pandas 列中提取字典列表 - How to extract list of dictionaries from Pandas column 从 Pandas 列中的元组列表中提取每个第一项 - extract each first item from list of tuples in pandas column 如何使用python从以列中数字开头的字符串中提取数字 - how to extract numbers from a string that starts with numbers in a column using python Pandas DataFrame:如何从并非总是以两个数字结尾的列中提取最后两个字符串类型的数字 - Pandas DataFrame: How to extract the last two string type numbers from a column which doesn't always end with the two numbers 如何从“对象”的 pandas 列中的字符串中提取数字? - How do I extract numbers from the strings in a pandas column of 'object'? 提取 pandas 列中每个单词的第一个字符串 - Extract first string of each word in pandas column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM