【发布时间】:2016-06-18 03:28:32
【问题描述】:
我在下面的数据框中有两个系列。第一个是一个字符串,它将出现在第二个中,它将是一个 url 字符串。我想要做的是通过连接额外的字符来更改第一个系列,并将该更改应用于第二个字符串。
import pandas as pd
#import urlparse
d = {'OrigWord' : ['bunny', 'bear', 'bull'], 'WordinUrl' : ['http://www.animal.com/bunny/ear.html', 'http://www.animal.com/bear/ear.html', 'http://www.animal.com/bull/ear.html'] }
df = pd.DataFrame(d)
def trial(source_col, dest_col):
splitter = dest_col.str.split(str(source_col))
print type(splitter)
print splitter
res = 'angry_' + str(source_col).join(splitter)
return res
df['Final'] = df.applymap(trial(df.OrigWord, df.WordinUrl))
我正在尝试find the string from the source_col,然后在dest_col 中的那个字符串上split,然后对dest_col 中的字符串进行更改。在这里,我将它作为一个名为Final 的新系列,但我宁愿就地。我认为主要问题是 splitter 变量,它不起作用以及函数的应用。
结果应该是这样的:
OrigWord WordinUrl
angry_bunny http://www.animal.com/angry_bunny/ear.html
angry_bear http://www.animal.com/angry_bear/ear.html
angry_bull http://www.animal.com/angry_bull/ear.html
【问题讨论】:
标签: python pandas join split series