使用偶数列作为列名称格式化数据透视表

时间:2016-11-20 01:12:32

标签: python pandas pivot-table

我试图解析来自finviz报价个人资料页面的抓取数据。我想我可以使用panda的数据透视表机制来获取我需要的输出,但从未使用数据透视表,所以我不确定是否或如何格式化输出。

我收到的表格如下。我希望每个偶数列号都是输出表的列标题。使用包含一行和72列的数据框结束,因为有72个值。除非有人能推荐更好的输出结构以及如何访问这些值?

    0   1   2   3   4   5   6   7   8   9   10  11
0   Index   S&P 500 P/E 23.06   EPS (ttm)   3.15    Insider Own 0.20%   Shs Outstand    1.01B   Perf Week   3.94%
1   Market Cap  73.24B  Forward P/E 21.29   EPS next Y  3.41    Insider Trans   -34.81% Shs Float   996.66M Perf Month  4.85%
2   Income  3.21B   PEG 2.31    EPS next Q  0.81    Inst Own    89.30%  Short Float 2.05%   Perf Quarter    4.47%
3   Sales   13.15B  P/S 5.57    EPS this Y  9.70%   Inst Trans  0.42%   Short Ratio 4.21    Perf Half Y 25.19%
4   Book/sh 10.26   P/B 7.08    EPS next Y  7.88%   ROA 20.10%  Target Price    74.34   Perf Year   28.65%
5   Cash/sh 3.11    P/C 23.35   EPS next 5Y 10.00%  ROE 32.10%  52W Range   45.51 - 72.28   Perf YTD    36.02%
6   Dividend    2   P/FCF   31.29   EPS past 5Y 1.50%   ROI 21.60%  52W High    0.44%   Beta    1.26
7   Dividend %  2.75%   Quick Ratio 2.5 Sales past 5Y   -1.40%  Gross Margin    60.70%  52W Low 59.54%  ATR 1.34
8   Employees   29977   Current Ratio   3.3 Sales Q/Q   7.20%   Oper. Margin    35.20%  RSI (14)    67.26   Volatility  1.65% 1.93%
9   Optionable  Yes Debt/Eq 0.35    EPS Q/Q 23.80%  Profit Margin   24.40%  Rel Volume  1.13    Prev Close  72.08
10  Shortable   Yes LT Debt/Eq  0.29    Earnings    Oct 26 AMC  Payout  47.70%  Avg Volume  4.84M   Price   72.6
11  Recom   2.5 SMA20   3.86%   SMA50   4.97%   SMA200  16.49%  Volume  5476883 Change  0.72%

我知道格式很难看,所以 enter image description here

1 个答案:

答案 0 :(得分:3)

尝试重塑

d1 = pd.DataFrame(df.values.reshape(-1, 2), columns=['key', 'value'])
d1.set_index('key').T