pandas 说我需要一个finvizfinance的索引,但是当我添加一个index = 0时,它说我的集合是零?

toe95027  于 2023-03-16  发布在  其他
关注(0)|答案(3)|浏览(133)

我正在尝试使用Python通过finvizfinance库将股票信息导入到excel表格中。panda的数据框修正没有正确给出一个可以放入excel的格式。这是怎么回事?我需要将索引设置为一个列表,该列表等于finvizfinance库中所有行的输出(例如“公司”、“行业”等)吗?如果有任何帮助,我将不胜感激。
当我简单地打印stock_fundament时,会得到如下所示的结果...
{“公司”:“特斯拉公司”,“行业”:“消费周期”、“行业”:“汽车制造商”,......,“SMA 20”:“-11.37%”,“SMA50”:“4.62%”,“SMA200”:“-20.45%”,“体积”:“167,302,066”,“变更”:百分之零点六
空数据框列:[a、B、c、d、e、f、g、h、i、j]索引:[]

from finvizfinance.quote import finvizfinance
import pandas as pd
 
stock = finvizfinance('tsla')
stock_fundament = stock.ticker_fundament()
 
 
# Create a Pandas dataframe from the data.
df = pd.DataFrame(stock_fundament, index=0, columns=['a','b','c','d','e','f','g','h','i','j'])
print(stock_fundament)
print(df)

#df = df[list(df.columns[~df.columns.duplicated()])]         
#This was used to delete potential duplicate columns

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter(r'C:\Users\ncwes\AppData\Local\Programs\Python\Python310\pandas_simple.xlsx')
 
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')
 
# Close the Pandas Excel writer and output the Excel file.
writer.close()
mlnl4t2r

mlnl4t2r1#

您可以使用pd.json_normalize(stock_fundament)来正确读入。

df = pd.DataFrame(pd.json_normalize(stock_fundament))

print(df)

       Company             Sector  ...       Volume Change
0  Tesla, Inc.  Consumer Cyclical  ...  142,799,036  5.03%
wsxa1bj1

wsxa1bj12#

index中缺少[]括号:

df = pd.DataFrame(stock_fundament, index=[0])

df给我:

Company             Sector            Industry Country         Index    P/E EPS (ttm)  ...   Price Recom   SMA20  SMA50   SMA200       Volume Change
0  Tesla, Inc.  Consumer Cyclical  Auto Manufacturers     USA  NDX, S&P 500  48.13      3.62  ...  183.26  2.40  -6.38%  9.08%  -16.38%  142,763,793  5.03%

[1 rows x 78 columns]

如果我们使用您提供的列,您将得到以下内容:

df = pd.DataFrame(stock_fundament, index=[0], columns=['a','b','c','d','e','f','g','h','i','j'])

df现在是:

a    b    c    d    e    f    g    h    i    j
0  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
ffvjumwh

ffvjumwh3#

谢谢大家!在索引中添加括号并删除列修复了它。

相关问题