【发布时间】:2019-04-01 11:44:36
【问题描述】:
嗨,我有 2 个非常大的 csv 文件
df1
x y z keywords
a b c [apple,iphone,watch,newdevice]
e w q NaN
w r t [pixel,google]
s t q [india,computer]
d j o [google,apple]
df2
name stockcode
apple.inc appl
lg.inc weew
htc.inc rrr
google.com ggle
现在我需要检查 df1 中的 m 值与 df2 中的新值是否匹配,我需要将新值的详细信息合并到 df1 中,否则我们需要填充空值
我需要用python请帮帮我
样本输出
x y z keywords stockcode
a b c [apple,iphone,watch,newdevice] aapl
e w q NaN null
w r t [pixel,google,] ggle
s t q [india,computer] null
d j o [google,apple] aapl,ggle
我已经编写了这段代码,但它只是比较一个关键字并给出一个股票代码,如果我们有 2 个在 df2 中匹配的关键字,我需要 2 个股票代码
df1['stockcode'] = np.nan
#mapping data
for indexKW,valueKW in df1.keyword.iteritems():
for innerVal in valueKW.split():
for indexName, valueName in df2['Name'].iteritems():
for outerVal in valueName.split():
if outerVal.lower() == innerVal.lower():
df1['stockcode'].loc[indexKW] = df2.Identifier.loc[indexName]
上述程序的输出
x y z keywords stockcode
a b c [apple,iphone,watch,newdevice] aapl
e w q NaN null
w r t [pixel,google,] ggle
s t q [india,computer] null
d j o [google,apple] ggle
对于最后一行,我有 2 个在 df2 中匹配的关键字,但我只得到一个匹配的关键字 google 的股票代码,我还需要获取苹果的股票代码,如示例输出中所示。
样本输出:-
x y z keywords stockcode
a b c [apple,iphone,watch,newdevice] aapl
e w q NaN null
w r t [pixel,google,] ggle
s t q [india,computer] null
d j o [google,apple] aapl,ggle
请帮帮我
【问题讨论】:
-
请检查我的解决方案和
upvote and accept,如果有任何解决方案提供了您所需的结果。