[英]How to extract specific value from excel column using python pandas dataframe
Need to extract specific value from excel column using python pandas dataframe需要使用 python pandas dataframe 从 excel 列中提取特定值
The column Product that I am trying to extract looks the below & need to extract only Product # from it.我要提取的产品列如下所示,需要从中仅提取产品编号。 The column also has other numbers but the Product # always comes after the term 'UK Pro' & Product # could be 3 to 4 digit number in a particular row of data.
该列还有其他数字,但产品编号始终出现在术语“UK Pro”之后,并且产品编号可能是特定数据行中的 3 到 4 位数字。
In[1]:在[1]中:
df['Product'].head() df['产品'].head()
#Dataframe looks like this: #Dataframe 看起来像这样:
Out[1]:出[1]:
Checking center: King 2000: UK Pro 1000: London检查中心:King 2000:UK Pro 1000:London
Checking center: Queen 321: UK Pro 250: Spain检查中心:Queen 321:英国 Pro 250:西班牙
CC: UK Pro 3000: France抄送:英国 Pro 3000:法国
CC: UK Pro 810: Poland抄送:英国 Pro 810:波兰
Expected Output:预计 Output:
Product #产品 #
1000 1000
250 250
3000 3000
810 810
Started with this:从这个开始:
df['Product #'] = df1['Product'].str.split(':').str[1] df['产品编号'] = df1['产品'].str.split(':').str[1]
But this does split only based on the first two occurrence of: operator.但这确实仅根据前两次出现的操作符进行拆分。
Then tried this:然后尝试了这个:
df1['Product #'] = df1['Product'].str.split('UK Pro', 1).str[0].str.strip() df1['Product #'] = df1['Product'].str.split('UK Pro', 1).str[0].str.strip()
You can use pandas.Series.str.extract
:您可以使用
pandas.Series.str.extract
:
df["Product #"] = df["Product"].str.extract("UK Pro (\d+)", expand=False)
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.