python - 如何在一行上获取 Python 的 Pandas 数据框输出
问题描述
我有一个提取财务信息的程序。
当我运行程序时,它会吐出一个数据框,将数据框的最后一列推到下一行。
我希望数据框的所有列都是一行。
我尝试将终端的屏幕缓冲区大小更改为 200。
我试过通过终端窗口调整大小。
代码:
import requests
import pandas as pd
import timeit
start_time = timeit.default_timer()
pd.options.display.max_columns = None
pd.options.display.max_rows = None
def getdata(stock):
# Company Quote Group of Items
company_quote = requests.get(f"https://financialmodelingprep.com/api/v3/quote/{stock}")
company_quote = company_quote.json()
share_price = float("{0:.2f}".format(company_quote[0]['price']))
# Balance Sheet Group of Items
BS = requests.get(f"https://financialmodelingprep.com/api/v3/financials/balance-sheet-statement/{stock}?period=quarter")
BS = BS.json()
# Total Cash
cash = float("{0:.2f}".format(float(BS['financials'][0]['Cash and short-term investments'])/10**9))
# Total Debt
debt = float("{0:.2f}".format(float(BS['financials'][0]['Total debt'])/10**9))
# Income Statement Group of Items
IS = requests.get(f"https://financialmodelingprep.com/api/v3/financials/income-statement/{stock}?period=quarter")
IS = IS.json()
# Most Recent Quarterly Revenue
qRev = float("{0:.2f}".format(float(IS['financials'][0]['Revenue'])/10**9))
# Company Profile Group of Items
company_info = requests.get(f"https://financialmodelingprep.com/api/v3/company/profile/{stock}")
company_info = company_info.json()
# Chief Executive Officer
ceo = company_info['profile']['ceo']
return (share_price, cash, debt, qRev, ceo)
tickers = ('AAPL', 'MSFT', 'GOOG', 'T', 'CSCO', 'INTC', 'ORCL', 'AMZN', 'FB', 'TSLA', 'NVDA')
data = map(getdata, tickers)
df = pd.DataFrame(data,
columns=['Share Price', 'Total Cash', 'Total Debt', 'Q3 2019 Revenue', 'CEO'],
index=tickers)
print(df), print('\n')
print('Total time (Default Timer) = {:.5f} seconds.'.format(timeit.default_timer() - start_time))
输出:1
解决方案
推荐阅读
- javascript - React 中用于访问令牌和刷新令牌的 Axios 拦截器
- powershell - 我的 Azure 自动化 Runbook 使用存储的凭据来获取新组的列表,需要不使用存储凭据的替代方案
- git - 在 Intellij / Android Studio 中,如何更改 diff gutter 列的分支差异?
- java - 使用测试容器模拟数据库的网络中断
- tensorflow - Sketch_RNN,ValueError:无法输入形状的值
- prolog - 理解 Prolog 中的否定
- python - 递归自连接和转置
- python - 从 PDB 文件中查找氨基酸索引和序列之间的映射
- r - 数据表中的累积和,但对每一行使用分组
- azure - 增加 ARM CustomScriptExtension 日志大小