簡體   English   中英

如何從行值中刪除除 int 的第一個實例之外的所有內容?

[英]How to strip everything from a row value except the first instance of an int?

如果我有這樣的行值:

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])
import re

list = ['Blow Balloons 5 Yes 4', 
    '3 Shirt No', 
    'Goodbye Something 2 1']

regex = re.compile("(\d){1}")

for str in list:
    data = regex.search(str).group()
    print(data)

Output:

5
3
2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM