Python 除了int的第一个实例外,如何从行值中删除所有内容?

Python 除了int的第一个实例外,如何从行值中删除所有内容?,python,pandas,dataframe,Python,Pandas,Dataframe,如果我有这样的行值: Blow Balloons 5 Yes 4 3 Shirt No Goodbye Something 2 1 那么我只希望它是: 5 3 2 那么int的第一个实例,我怎么做呢?我不能使用正则表达式,因为没有数字首次出现的模式。这里有一个可能的解决方案。它实际上使用正则表达式: import pandas as pd data = ['Blow Balloons 5 Yes 4', '3 Shirt No', 'Goodbye So

如果我有这样的行值:

Blow Balloons 5 Yes 4
3 Shirt No 
Goodbye Something 2 1
那么我只希望它是:

5
3
2

那么int的第一个实例,我怎么做呢?我不能使用正则表达式,因为没有数字首次出现的模式。

这里有一个可能的解决方案。它实际上使用正则表达式:

import pandas as pd
data = ['Blow Balloons 5 Yes 4', 
        '3 Shirt No', 
        'Goodbye Something 2 1']
data = pd.Series(data)
data = data.str.replace(r'\D+', ' ').str.split()
data = data.apply(lambda x: x[0])
输出:

5
3
2

df[“header”].str.extract(r“(\d)”)
?向我们展示您迄今为止的代码。出于某种原因,我得到的索引超出了lambda-I打印数据的范围,并且它看起来像是预期的,列是1个元素的列表?我必须像lambdata那样执行“”。join(x)这是因为有些行没有int。可以在lambda函数中使用if语句,如:data.apply(lambda x:x[0]if len(x)>0 else“”)如果数大于9,请小心使用“”。join(x)。它将只保留第一个字符。如果只有介于0和9之间的数字,则可以使用此解决方案:data.str.replace(r'\D+','').str[0]
5
3
2