2
假設我有describe()
用MultiIndex
在列索引產生DataFrame
:此數據框中的數據透視/重建索引列MultiIndex?
txid FOOBAR
count mean std min 25% 50% 75% max
meas 0 233.0 8.064378 76.225789 -127.0 -62.0 14.0 78.0 126.0
...
txid DEADBEEF
count mean std min 25% 50% 75% max
meas 0 60.0 7.866667 78.921215 -127.0 -55.75 3.0 81.75 126.0
...
我怎樣才能彈出「TxID添加」按下按鍵與兩倍多行的DataFrame
:
txid count mean std min 25% 50% 75% max
meas 0 FOOBAR 233.0 8.064378 76.225789 -127.0 -62.0 14.0 78.0 126.0
...
meas 0 DEADBEEF 60.0 7.866667 78.921215 -127.0 -55.75 3.0 81.75 126.0
UPDATE
略有改善,回答@jezrael提供:
df1 = df.groupby('txid').apply(lambda x: x.describe())
# start improvements
df1.index.rename('idx',level=1,inplace=True)
df1.reset_index(inplace=True)
df1 = df1.pivot(columns='txid',index='idx')
df1 = df1.T
df.index.rename('meas',level=0,inplace=True)
也許用'pd.pivot_table'? –