嗨,我有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
现在我需要用df2中的新值检查df1中的m值,如果它匹配,我需要将新值的细节合并到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个股票代码,如果我们有2个关键字是在df2匹配
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
对于最后一行,我有两个关键字在df2中匹配,但我只得到一个关键字google的匹配stockcode,我还需要得到苹果的stockcode,如示例输出所示。你知道吗
样品输出:-你知道吗
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
请帮帮我伙计们
可以将
apply
和map
与join
一起用作:或:
或:
可以将df2转换为查找字典,然后将其映射到df1;)
相关问题 更多 >
编程相关推荐