首页 > 解决方案 > 需要帮助将 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)

标签: pythonpandas

解决方案


推荐阅读