如何在Python中透视Pandas Dataframe?

时间:2019-01-11 05:49:26

标签: python python-3.x python-2.7

我正在尝试透视以下数据框。我希望将列名添加为行。第一行是一个状态,但列名不是静态的,因为它们将针对数据框中的所有数字列进行计算。你能帮忙吗?

这是我的数据框:

enter image description here

预期数据框:

enter image description here

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:2)

您只需添加.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 :(得分:0)

其他方法是 <writer id="indexer_solr_1" class="org.apache.nutch.indexwriter.solr.SolrIndexWriter"> <parameters> <param name="type" value="cloud"/> <param name="url" value="http://localhost:8983/solr"/> <param name="collection" value="nutch"/> <param name="weight.field" value=""/> <param name="commitSize" value="1000"/> <param name="auth" value="true"/> <param name="username" value="solr"/> <param name="password" value="password"/> </parameters> <mapping> <copy> <!-- <field source="content" dest="search"/> --> <!-- <field source="title" dest="title,search"/> --> </copy> <rename> <field source="metatag.description" dest="description"/> <field source="metatag.keywords" dest="keywords"/> </rename> <remove> <field source="segment"/> </remove> </mapping> </writer>

.transpose()