第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從列中的字符串中提取一組 n 個數(shù)字

從列中的字符串中提取一組 n 個數(shù)字

SMILET 2021-10-19 09:33:53
我在 Pandas 數(shù)據(jù)框中有一列字符串,其中包含以下內(nèi)容:"AU/4347001"但此外還有其他組織較少的字符串,例如"Who would have thought this would be so 4347009 difficult"因此,最終,對于這些數(shù)字系列將出現(xiàn)在字符串中的位置和方式,沒有一致的模式。它們可能在開頭、中間或結(jié)尾,并且無法確切知道數(shù)字周圍有多少其他字符。理想情況下,我想返回另一列僅包含數(shù)字的等長列。這可能嗎?任何幫助是極大的贊賞!
查看完整描述

3 回答

?
慕雪6442864

TA貢獻1812條經(jīng)驗 獲得超5個贊

你可以這樣做extract:


df =pd.DataFrame({'text':["Who would have thought this would be so 4347009 difficult",

                          "24 is me"]})


df['new_col'] = df['text'].str.extract(r'(\d+)')


    text                                                new_col

0   Who would have thought this would be so 434700...   4347009

1   24 is me    


查看完整回答
反對 回復 2021-10-19
?
人到中年有點甜

TA貢獻1895條經(jīng)驗 獲得超7個贊

您可以將提取與數(shù)字的捕獲組一起使用(\d+):


import pandas as pd


data = ["AU/4347001",

        "Who would have thought this would be so 4347009 difficult",

        "Another with a no numbers",

        "131242143"]


df = pd.DataFrame(data=data, columns=['txt'])

result = df.assign(res=df.txt.str.extract('(\d+)')).fillna('')

print(result)

輸出


                                                 txt        res

0                                         AU/4347001    4347001

1  Who would have thought this would be so 434700...    4347009

2                          Another with a no numbers           

3                                          131242143  131242143

注意,在上面的例子中,使用fillna來填充那些沒有找到數(shù)字組的列,在這種情況下,用空字符串填充。


查看完整回答
反對 回復 2021-10-19
?
湖上湖

TA貢獻2003條經(jīng)驗 獲得超2個贊

這是我們的測試 DataFrame:


### Create an example Pandas Dataframe

df = pd.DataFrame(data=['something123', 'some456thing', '789somthing', 

                        'Lots of numbers 82849585 make a long sentence'], columns = ['strings'])


### Create a function for identifying, joining and then turning the string to an integer

def get_numbers(string):

    return int(''.join([s for s in string if s.isdigit()]))


### Now lets apply the get_numbers function to the strings column

df.loc[:,'strings_wo_numbers'] = df.loc[:,'strings']apply(get_numbers)

注意:這將連接字符串中的所有數(shù)字,即“10 個橄欖和 5 個蘋果”將變成 105 而不是 10、5。


查看完整回答
反對 回復 2021-10-19
  • 3 回答
  • 0 關注
  • 185 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號