简体   繁体   English

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

[英]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)

# Output: #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.

相关问题 从 pandas Dataframe 中的 JSON 列中提取特定值 - Extract specific value from JSON column in pandas Dataframe 如何提取Python pandas数据帧的特定列的特定位 - How to extract a particular bits of a specific column of Python pandas dataframe 如何使用excel路径列表中的python pandas创建一个特定Excel信息的大型数据框 - How to create one large dataframe of specific excel information using python pandas from a list of excel paths 如何使用 Python Pandas 从 1 行中的特定列获取值? - How to get value from specific column in 1 row using Python Pandas? 如何使用 pandas dataframe 从带有字典列表的列中提取值 - How to extract value out of column with list of dictionary using pandas dataframe 如何从 Python Pandas Dataframe 的 STRING 列中提取嵌套字典? - How to extract a nested dictionary from a STRING column in Python Pandas Dataframe? 如何从 Pandas Python 中 DataFrame 中的列中的字符串中提取一些值? - How to extract some values from string in column in DataFrame in Pandas Python? 如何从 python pandas dataframe 的列中的列表中提取字符串? - How to extract strings from a list in a column in a python pandas dataframe? Python:如何从熊猫数据框列中提取多个字符串 - Python: How to extract multiple strings from pandas dataframe column 如何从 python pandas 数据框中的列中的列表中提取关键字(字符串)? - How to extract keywords (strings) from a list in a column in a python pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM