熊猫-是否可以通过多列字符串映射?

时间:2018-11-21 12:04:16

标签: python pandas dataframe

我有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

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

我遵循了以下建议,并出现了ValueError

----> 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)

0 个答案:

没有答案
相关问题