有没有可能用多列的字符串进行映射?

2024-09-30 10:36:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有3个这样的数据框,每个数据框的形状大小不一样

df
fruits           priceyear_2010
orange                15
apple               10
watermelon            3
melon                 7
strawberry           11

df1
fruits           priceyear_2011
watermelon           5
apple                4
strawberry          19

df2
fruits           priceyear_2012
apple                12
orange               16
watermelon           14
melon                18

我想用字符串映射得到这样的结果

df_result
fruits           priceyear_2010  priceyear_2011 priceyear_2012
apple                10              4            12
orange               15              Nan          16
watermelon            3              5            14
melon                 7              Nan          18
strawberry           11             19            Nan

很抱歉问这个,但我不知道

我遵循了下面的建议,得到了错误的值

----> 2 df = pd.concat([x.set_index(['fruits']) for x in dfs], axis = 1)

~/anaconda3/lib/python3.6/site-packages/pandas/core/reshape/concat.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, sort, copy)
    224                        verify_integrity=verify_integrity,
    225                        copy=copy, sort=sort)
--> 226     return op.get_result()
    227 
    228 


ValueError: Shape of passed values is (3, 2285), indices imply (3, 2284)

Tags: 数据appledfnansortmeloncopyverify

热门问题