将多列数据框转换为系列

时间:2018-05-09 23:59:18

标签: python pandas dataframe

我想将以下内容转换为系列文章。

       Trades Before  Trades After    Predicted    Change
count   31540.000000   1000.000000  1000.000000 -0.968294
mean       39.151712     42.216000    90.144811  0.078267
std       130.948917    143.153156     1.345089  0.093198
min     -1611.000000  -1371.000000    88.234987 -0.148976
25%        29.000000     34.000000    89.052902  0.172414
50%        74.000000     79.000000    89.979200  0.067568
75%        99.000000    109.000000    91.127657  0.101010
max       184.000000    179.000000    93.915568 -0.027174

索引是行名称和列名称的组合,例如:

Trades After 50%    79.000000    

1 个答案:

答案 0 :(得分:1)

您可以使用pandas.melt取消隐藏数据框。在这种特殊情况下,您需要将索引提升为列:

res = pd.melt(df.reset_index(), id_vars=['index'])

结果:

print(res)

    index      variable         value
0   count  TradesBefore  31540.000000
1    mean  TradesBefore     39.151712
2     std  TradesBefore    130.948917
3     min  TradesBefore  -1611.000000
4     25%  TradesBefore     29.000000
5     50%  TradesBefore     74.000000
6     75%  TradesBefore     99.000000
7     max  TradesBefore    184.000000
8   count   TradesAfter   1000.000000
9    mean   TradesAfter     42.216000
10    std   TradesAfter    143.153156
11    min   TradesAfter  -1371.000000
12    25%   TradesAfter     34.000000
13    50%   TradesAfter     79.000000
14    75%   TradesAfter    109.000000
15    max   TradesAfter    179.000000
16  count     Predicted   1000.000000
17   mean     Predicted     90.144811
18    std     Predicted      1.345089
19    min     Predicted     88.234987
20    25%     Predicted     89.052902
21    50%     Predicted     89.979200
22    75%     Predicted     91.127657
23    max     Predicted     93.915568
24  count        Change     -0.968294
25   mean        Change      0.078267
26    std        Change      0.093198
27    min        Change     -0.148976
28    25%        Change      0.172414
29    50%        Change      0.067568
30    75%        Change      0.101010
31    max        Change     -0.027174