簡體   English   中英

如何使用 python pandas dataframe 從 excel 列中提取特定值

[英]How to extract specific value from excel column using python pandas dataframe

需要使用 python pandas dataframe 從 excel 列中提取特定值

我要提取的產品列如下所示,需要從中僅提取產品編號。 該列還有其他數字,但產品編號始終出現在術語“UK Pro”之后,並且產品編號可能是特定數據行中的 3 到 4 位數字。

在[1]中:

df['產品'].head()

#Dataframe 看起來像這樣:

出[1]:

檢查中心:King 2000:UK Pro 1000:London

檢查中心:Queen 321:英國 Pro 250:西班牙

抄送:英國 Pro 3000:法國

抄送:英國 Pro 810:波蘭

預計 Output:

產品 #

1000

250

3000

810

從這個開始:

df['產品編號'] = df1['產品'].str.split(':').str[1]

但這確實僅根據前兩次出現的操作符進行拆分。

然后嘗試了這個:

df1['Product #'] = df1['Product'].str.split('UK Pro', 1).str[0].str.strip()

您可以使用pandas.Series.str.extract

df["Product #"] = df["Product"].str.extract("UK Pro (\d+)", expand=False)

#Output:

print(df)
   Product #
0        NaN
1        NaN
2       1000
3        NaN
4        NaN
5        250
6        NaN
7       3000
8        NaN
9        810
10       NaN

暫無
暫無

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

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