通用方法
pandas.melt => 选择一个列 变量为列索引 值为列数据
df A B C 0 a 1 2 1 b 3 4 2 c 5 6
pd.melt(df, id_vars=['A'], value_vars=['B'], var_name='myVarname', value_name='myValname') A myVarname myValname 0 a B 1 1 b B 3 2 c B 5
pandas.pivot =>生成一个交叉表 取一个列作为行 一个列作为新的列 取一个列作为交叉的值(不允许交叉重复的情况)
foo bar baz zoo 0 one A 1 x 1 one B 2 y 2 one C 3 z 3 two A 4 q 4 two B 5 w 5 two C 6 t
df.pivot(index='foo', columns='bar', values='baz')
bar A B C foo
one 1 2 3 two 4 5 6
pandas.pivot_table=>对重复交叉进行 一个函数计算
df A B C D E 0 foo one small 1 2 1 foo one large 2 4 2 foo one large 2 5 3 foo two small 3 5 4 foo two small 3 6 5 bar one large 4 6 6 bar one small 5 8 7 bar two small 6 9 8 bar two large 7 9
table = pd.pivot_table(df, values='D', index=['A', 'B'], columns=['C'], aggfunc=np.sum)
C large small A B bar one 4.0 5.0 bar two 7.0 6.0 foo one 4.0 1.0 foo two NaN 6.0
pandas.merge =>合并两个df
标签:bar,df,two,large,small,数据处理,foo,Pandas From: https://www.cnblogs.com/xzqpy/p/16625224.html