簡體   English   中英

從 Pandas 數據框的字符串列中提取特定值

[英]Extract specific value from the string column of a Pandas Data frame

我是 Python 的新手。 我從通過 excel 工作表的插件中獲取數據,我需要從該列中提取值。

  Plugin Output

 Country:USA   State: Virginia Address: 23 xys lane  SSN:2345550404  Zip : 22102 City: Fairfax

 Country:India State:Virginia  SSN:2345550401  ZIP:452002  City: Indore

我需要在每一行中搜索 SSN 並在新的 pandas 數據框中創建一個新列以創建一個單獨的列。

所需的 Output:

  SSN

 2345550404

 2345550401

序列號答案:

def find_serialnumber(x):
  num = re.findall(r'Serial Number:\s*([^\n]+)', x)
  return " ".join(num)
import re

    def find_number(x):
        num = re.findall(r'(?:SSN_)(\d+)', x)
        return " ".join(num)

    df['SSN'] =df['Output'].apply(lambda x: find_number(x))

還要從 pandas 中提取 function:

所以 \d+ 表示匹配 1 個或多個數字。

df['SSN'] = df['Output'].apply(lambda x: re.findall(r'(?:SSN_)(\d+)', x)[0] if re.findall(r'(?:SSN_)(\d+)', x) else x)

暫無
暫無

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

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