从数据框中的字符串中提取子字符串 [英] Extract substring from string in dataframe
本文介绍了从数据框中的字符串中提取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下ddataframe:
I have the following ddataframe:
Company Name Time Expectation
0 Asta Funding Inc. (ASFI) 9:35 AM ET -
1 BlackBerry (BBRY) 7:00 AM ET ($0.03)
2 Carnival Corp. (CCL) 9:15 AM ET $0.09
3 Carnival PLC (CUK) 0:00 AM ET -
我希望公司符号放在其自己的单独列中,而不是在公司名称"列中.现在,我只需遍历公司名称,然后RE提取符号,将其放入列表中,然后将其应用于新列,但是我想知道是否有更清洁/更简便的方法.
I would like to have the company symbols in their own seperate column instead of inside the Company Name column. Right now I just have it iterate over the company names, and a RE pulls the symbols, puts it into a list, and then I apply it to the new column, but I'm wondering if there is a cleaner/easier way.
我是整个地图中减少lambda内容的新手.
I'm new to the whole map reduce lambda stuff.
for company in df['Company Name']:
ticker = re.search("\(.*\)",company).group(0)
ticker = ticker[1:len(ticker)-1]
tickers.append(ticker)
推荐答案
Regex搜索已内置在熊猫的Series类中.您可以找到文档
Regex search is built into the Series class in pandas. You can find the documentation here. In your case, you could use
df['ticker'] = df['Company Name'].str.extract("\((.*)\)")
这篇关于从数据框中的字符串中提取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文