在Python中,搜索一列以包含一个值,从anotherdf中,将anotherdf列中的值添加到originaldf中的新列中
与此类似: 但不搜索df1,而是从公共目录中搜索df2 res=df1 数据帧_TPV=df2在Python中,搜索一列以包含一个值,从anotherdf中,将anotherdf列中的值添加到originaldf中的新列中,python,pandas,numpy,dataframe,Python,Pandas,Numpy,Dataframe,与此类似: 但不搜索df1,而是从公共目录中搜索df2 res=df1 数据帧_TPV=df2 res['PCR'] = (dataframe_TPV.ObjectID .apply(lambda x: dataframe_TPV[res.RollupResult.str.find(x).ge(0)]['OtherPCR']) .bfill(axis=1) .iloc[:, 0]) 这给了我“TypeError:应该是字符串对象
res['PCR'] = (dataframe_TPV.ObjectID
.apply(lambda x: dataframe_TPV[res.RollupResult.str.find(x).ge(0)]['OtherPCR'])
.bfill(axis=1)
.iloc[:, 0])
这给了我“TypeError:应该是字符串对象,而不是float”
我试过这个:
def strmerge(strcolumn):
for i in res['Rollup Result']:
if strcolumn in i:
return dataframe_TPV[res['Rollup Result'] == i]['PCR'].values[0]
break
else:
pass
res['Other PCR'] = dataframe_TPV.apply(lambda x: strmerge(x['Object ID']),axis=1)
在返回调用中显示“IndexError:(‘索引0超出大小为0的轴0的界限’,‘发生在索引5’)”
下一行我将用换行符聚合列
df1['Other PCR'] = df1[['AllPCR', 'Other PCR]].agg('\n'.join, axis=1)
我该怎么做