我有一个返回字典的函数,我想使用apply将新列添加到现有数据框
import pandas as pd
from chainmap import ChainMap
# features1 is a dictionary returned from feature_func1
#features2 is a dictionary returned from feature_func2
def applyfeatures(x,datacolumn1,datacolumn2):
features1=feature_func1(x[datacolumn1],x[datacolumn2])
features2=feature_func2(x[datacolumn1],x[datacolumn2])
z= ChainMap(features1,features2)
return z
if __name__ == '__main__':
df=pd.read_csv(input_file)
datacolumn1="data1"
datacolumn2="data2"
newdf=df.apply(applyfeatures,args=(datacolumn1,datacolumn2),axis=1)
merged=df.join(newdf)
这会出错“其他系列必须有名称”。这可能是由于我从applyfeatures返回字典的方式。我应该如何修改它以基于返回的字典项创建新的数据帧。