代码之家  ›  专栏  ›  技术社区  ›  Shankar Panda

如何在python中透视熊猫数据帧?[复制品]

  •  1
  • Shankar Panda  · 技术社区  · 6 年前

    我正在尝试旋转下面的数据帧。我希望列名称作为行添加。第一行是statis列,但列名称不是静态的,因为它们将从数据框中为所有数值列计算。你能帮忙吗?

    这是我的数据框架:

    enter image description here

    预期的数据帧:

    enter image description here

    3 回复  |  直到 6 年前
        1
  •  1
  •   Prayson W. Daniel    6 年前

    您只需添加.t:)df.describe().t来转换结果:

    import pandas as pd
    import numpy as np
    
    #Create a Dictionary of series
    d = {'Name':pd.Series(['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine',
       'Rahul','David','Andrew','Ajay','Teresa']),
       'Age':pd.Series([26,27,25,24,31,27,25,33,42,32,51,47]),
       'Score':pd.Series([89,87,67,55,47,72,76,79,44,92,99,69])}
    
    #Create a DataFrame
    pd.DataFrame(d).describe().T
    

    结果: enter image description here

        2
  •  2
  •   Jetman user13961676    6 年前
        3
  •  0
  •   U13-Forward    6 年前

    其他方式是 .transpose() :

    data_pivot = data_pd.transpose()