我知道使用groupby&;或pivot峎u table&;/或stack很容易做到这一点,但我似乎无法将其从base one中删除。我的笔记没能告诉我怎么做。我以为我已经接近了pandas文档中的pivot_表了,但连一级都做不到,更不用说2级了。因为我不想把任何东西加起来。我的笔记到处都是聚合。。。在
您有什么建议吗
创建第一个数据帧的代码:
df2 = pd.DataFrame({'CPC_qtr_root': {13: 0.13493790567404607,
14: 0.14353736611331172,
15: 0.10359919568913414,
16: 0.077153346715340618,
17: 0.066759430932458397,
39: 0.12067193385680651,
40: 0.049033000970486448,
41: 0.047640864406214359,
42: 0.040086869604689483,
43: 0.038795815932666726,
100: 0.11017683494905577,
101: 0.15510499735697988,
102: 0.16478351543691827,
103: 0.091894700285988867,
104: 0.0359603120618152},
'Country': {13: u'Afghanistan',
14: u'Afghanistan',
15: u'Afghanistan',
16: u'Afghanistan',
17: u'Afghanistan',
39: u'Albania',
40: u'Albania',
41: u'Albania',
42: u'Albania',
43: u'Albania',
100: u'Angola',
101: u'Angola',
102: u'Angola',
103: u'Angola',
104: u'Angola'},
'IncomeLevel': {13: 'Lower Income',
14: 'Lower Income',
15: 'Lower Income',
16: 'Lower Income',
17: 'Lower Income',
39: 'Upper Middle Income',
40: 'Upper Middle Income',
41: 'Upper Middle Income',
42: 'Upper Middle Income',
43: 'Upper Middle Income',
100: 'Lower Middle Income',
101: 'Lower Middle Income',
102: 'Lower Middle Income',
103: 'Lower Middle Income',
104: 'Lower Middle Income'},
'Rate': {13: 27.0,
14: 37.0,
15: 35.0,
16: 39.0,
17: 48.0,
39: 95.0,
40: 95.0,
41: 96.0,
42: 93.0,
43: 96.0,
100: 36.0,
101: 65.0,
102: 66.0,
103: 52.0,
104: 52.0},
'Year': {13: 2000,
14: 2001,
15: 2002,
16: 2003,
17: 2004,
39: 2000,
40: 2001,
41: 2002,
42: 2003,
43: 2004,
100: 2000,
101: 2001,
102: 2002,
103: 2003,
104: 2004}})
将^{} 与^{} 和^{} 一起使用:
获得混合类型:
^{pr2}$首先,可以将数据帧从宽到长进行融合,使用}作为值:
Year
和Country
作为id,IncomeLevel
,CPC_qtr_root
,和{然后可以旋转桌子:
^{pr2}$这将返回:
相关问题 更多 >
编程相关推荐