简体   繁体   English

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

[英]Format pivot table with even columns as column names

I'm trying to parse scraped data from finviz quote profile pages. 我正在尝试解析finviz报价配置文件页面中的抓取数据。 I think I can use panda's pivot table mechanisms to get the output I need, but have never used pivot tables so I'm unsure if or how to format the output. 我认为我可以使用panda的数据透视表机制来获取所需的输出,但从未使用过数据透视表,因此我不确定是否格式化输出。

The table I receive is below. 我收到的表如下。 I would like each even column number to be the column header of the output table. 我希望每个偶数列号都作为输出表的列标题。 Ending with a dataframe with one row, and 72 columns, as there are 72 values. 由于有72个值,因此以一个数据行结束,该数据帧具有一行和72列。 Unless anyone can recommend a better output strcuture and how to access the values? 除非有人可以推荐更好的输出结构,以及如何访问这些值?

    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%

I know the formatting is hard to see, so 我知道格式很难看,所以 在此处输入图片说明

Try reshaping 尝试重塑

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM