我想合并从给定函数创建的多个数据框。我尝试使用pd.concat,但收到错误消息:
TypeError:第一个参数必须是pandas对象的可迭代对象,您 传递了一个类型为“ DataFrame”的对象
这是我编写的代码:
def plot_percs(sims,name):
filter = name
session_to_leads = (sims['session_to_leads'].sum()/len(sims['session_to_leads']))*100
leads_to_opps = (sims['lead_to_opps'].sum()/len(sims['lead_to_opps']))*100
oops_to_comp = (sims['oops_to_comp'].sum()/len(sims['oops_to_comp']))*100
percentages = pd.DataFrame(data=[filter,session_to_leads,leads_to_opps,oops_to_comp]).transpose()
percentages.columns = ['filter','session_to_leads','leads_to_opps','opps_to_complete']
return percentages
pd.concat(plot_percs(originalsims,'original'),plot_percs(facebooksims,'facebook'))
这是两个数据框的示例 originalsims:
filter session_to_leads leads_to_opps opps_to_complete
0 original 16.28 9.47 1.92
facebooksims
filter session_to_leads leads_to_opps opps_to_complete
0 facebook 1.76 0.02 0
答案 0 :(得分:0)
为传递给[]
函数的DataFrame
的列表添加pd.concat
,也可以添加ignore_index=True
以避免在输出中出现重复的索引:
df = pd.concat([plot_percs(originalsims,'original'),
plot_percs(facebooksims,'facebook')], ignore_index=True)
我认为您的函数应该用mean
重写:
def plot_percs(sims,name):
c = ['session_to_leads', 'lead_to_opps', 'oops_to_comp']
d = {'oops_to_comp':'opps_to_complete'}
percentages = sims[c].mean().to_frame().T.rename(columns=d)
percentages.insert(0, 'filter', name)
return percentages