Pandas 提取值并从中创建新列
我想提取URL的某个部分,位于Pandas Dataframe的一列中,并使其成为一个新列。这个Pandas 提取值并从中创建新列,pandas,Pandas,我想提取URL的某个部分,位于Pandas Dataframe的一列中,并使其成为一个新列。这个 ref = df['REFERRERURL'] ref.str.findall("\\d\\d\\/(.*?)(;|\\?)",flags=re.IGNORECASE) 返回一个包含元组的序列。在创建序列之前,我如何只提取该元组的一部分,以便将其转换为列?refererURL的示例数据为 http://wap.blah.com/xxx/id/11/someproduct_step2;jsessio
ref = df['REFERRERURL']
ref.str.findall("\\d\\d\\/(.*?)(;|\\?)",flags=re.IGNORECASE)
返回一个包含元组的序列。在创建序列之前,我如何只提取该元组的一部分,以便将其转换为列?refererURL的示例数据为
http://wap.blah.com/xxx/id/11/someproduct_step2;jsessionid=....
在本例中,我感兴趣的是创建一个只包含“someproduct_step2”的列
谢谢
In [25]: df = DataFrame([['http://wap.blah.com/xxx/id/11/someproduct_step2;jsessionid=....']],columns=['A'])
In [26]: df['A'].str.findall("\\d\\d\\/(.*?)(;|\\?)",flags=re.IGNORECASE).apply(lambda x: Series(x[0][0],index=['first']))
Out[26]:
first
0 someproduct_step2
在0.11.1中,也有一种简洁的方法
In [34]: df.replace({ 'A' : "http:.+\d\d\/(.*?)(;|\\?).*$"}, { 'A' : r'\1'} ,regex=True)
Out[34]:
A
0 someproduct_step2
在0.11.1中,也有一种简洁的方法
In [34]: df.replace({ 'A' : "http:.+\d\d\/(.*?)(;|\\?).*$"}, { 'A' : r'\1'} ,regex=True)
Out[34]:
A
0 someproduct_step2
这也起了作用
def extract(x):
res = re.findall("\\d\\d\\/(.*?)(;|\\?)",x)
if res: return res[0][0]
session['RU_2'] = session['REFERRERURL'].apply(extract)
这也起了作用
def extract(x):
res = re.findall("\\d\\d\\/(.*?)(;|\\?)",x)
if res: return res[0][0]
session['RU_2'] = session['REFERRERURL'].apply(extract)