交换 pandas 轴 [英] Swapping Axes in Pandas

查看:55
本文介绍了交换 pandas 轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

交换熊猫数据框的轴的最有效方法是什么?

What is the most efficient way to swap the axes of a Pandas Dataframe?

例如,如何将df1转换为下面的df2?

For example, how could df1 be converted to df2 below?

In [2]: import pandas as pd

In [3]: df1 = pd.DataFrame({'one' : [1., 2., 3., 4.], 'two' : [4., 3., 2., 1.]})

In [4]: df1
Out[4]: 
   one  two
0    1    4
1    2    3
2    3    2
3    4    1

In [5]: df2 = pd.DataFrame({0 : [1,4], 1 : [2,3], 2 : [3,2], 3 : [4,1]}, index=['one','two'])

In [6]: df2
Out[6]: 
     0  1  2  3
one  1  2  3  4
two  4  3  2  1

推荐答案

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆