python - 需要帮助将 2 个切片索引绘制到 python 中的条形图中
问题描述
我是编码新手,我需要一些帮助来绘制从网络上抓取的数据。我相信我遇到的问题是我试图绘制的数据类型不正确,但我无法将它们转换为正确的类型(收入 -> 整数和日期 -> 字符串/日期时间)。任何帮助将不胜感激!如果有帮助,我在下面包含了我的代码。谢谢!
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
import lxml
from lxml import html
import requests
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
symbol = input('Enter Ticker: ') #Use AAPL/GOOG/any US based stock ticker you might know
#Balance Sheet Scrape
url_BS = 'https://finance.yahoo.com/quote/' + symbol + '/balance-sheet?p=' + symbol
page_BS = requests.get(url_BS, verify=False)
tree_BS = html.fromstring(page_BS.content)
table_BS = tree_BS.xpath('//table')
assert len(table_BS) == 1
df_BS = pd.read_html(lxml.etree.tostring(table_BS[0], method='html'))[0]
#BS End
#Profit&Loss Scrape
url_PL = 'https://finance.yahoo.com/quote/' + symbol + '/financials?p=' + symbol
page_PL = requests.get(url_PL, verify=False)
tree_PL = html.fromstring(page_PL.content)
table_PL = tree_PL.xpath('//table')
assert len(table_PL) == 1
df_PL = pd.read_html(lxml.etree.tostring(table_PL[0], method='html'))[0]
#PL End
#Cash Flow Scrape
url_CF = 'https://finance.yahoo.com/quote/' + symbol + '/cash-flow?p=' + symbol
page_CF = requests.get(url_CF, verify=False)
tree_CF = html.fromstring(page_CF.content)
table_CF = tree_CF.xpath('//table')
assert len(table_CF) == 1
df_CF = pd.read_html(lxml.etree.tostring(table_CF[0], method='html'))[0]
#CF End
df_BS
df_PL
df_CF
#Here is my problem: When I scrap the table into pandas.dataframe it returns outputs as "objects".
#I believe that Matplotlib can only plot integers and strings(?).
#The problem is, i don't know how to convert just a slice of an index (df_PL.iloc[]) into those things
date = (df_PL.iloc[0:1,1:5].values.T.tolist())
print(date)
rev = (df_PL.iloc[1:2,1:5].values.T.tolist())
print(rev)
plot(date,rev)
解决方案
推荐阅读
- list - dart 中的列表映射实际上是如何工作的?
- angular - 在通用组件中呈现输入组件的简单方法
- python - 使用 SQL 查询插入数据的 Python 函数
- opencl - OpenCL,了解 VectorAdd 程序
- .net-core - dotnet ef 无法执行,因为找不到指定的命令或文件
- swift - React Native App 在关闭 uiviewcontroller 时崩溃
- swift - 如何从 swift 中的字符串生成初始化调用
- ruby-on-rails - Rails - 验证一年中的唯一月份
- sql - 如果它与同一个表中的另一个列值匹配,则获取列值
- amazon-web-services - 如何允许用户将视频文件传输到 ec2 实例