[英]How can the data be read from a csv into a pandas dataframe, with multiple columns?
[英]Can't read pandas dataframe columns imported from JSON
我正在嘗試讀取從 JSON 文件導入的 apandas dataframe。
我收到以下錯誤:
The data does not contain a column named 'totalRevenue'.
The data does not contain a column named 'future_revenue'.
Traceback (most recent call last):
File "/Users/Blake/PycharmProjects/Project/venv/lib/python3.9/site-packages/pandas/core/indexes/base.py", line 3803, in get_loc
return self._engine.get_loc(casted_key)
File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 165, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 5745, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 5753, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'future_revenue'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/aidanschmidt/PycharmProjects/Project/main.py", line 66, in <module>
financial_data['future_expenses'] = financial_data['future_revenue'] * expense_ratio
File "/Users/Blake/PycharmProjects/Project/venv/lib/python3.9/site-packages/pandas/core/frame.py", line 3805, in __getitem__
indexer = self.columns.get_loc(key)
File "/Users/Blake/PycharmProjects/Project/venv/lib/python3.9/site-packages/pandas/core/indexes/base.py", line 3805, in get_loc
raise KeyError(key) from err
KeyError: 'future_revenue'
“totalRevenue”在 JSON 和 Pandas dataframe 中,所以我不確定是什么問題。
我為 totalRevenue 添加了錯誤處理,但它也失敗了,但是 future_revenue 的錯誤處理失敗了,因為它直到后來才創建。
這是我的代碼:
import requests
import json
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Replace YOUR_API_KEY with your own Alpha Vantage API key
api_key = 'YOUR API KEY'
# Get the ticker symbol from the user
ticker_symbol = input("Enter the ticker symbol of the company: ")
# Gathering all three financial statements
functions = ['INCOME_STATEMENT', 'BALANCE_SHEET', 'CASH_FLOW']
financial_data = {}
for function in functions:
# Specify the frequency of the data
interval = 'annual' # or 'quarter'
# Send a request to the Alpha Vantage API to gather the financial data
url = f'https://www.alphavantage.co/query?function={function}&symbol={ticker_symbol}&apikey={api_key}&interval={interval}'
response = requests.get(url)
# Check the response status code and the content of the response in case the API returns an error message
if response.status_code != 200:
print("Error: API request failed with status code", response.status_code)
print(response.content)
exit()
try:
financial_data[function] = json.loads(response.text)
except json.decoder.JSONDecodeError as e:
print("Error: Failed to parse JSON data from API response")
print(e)
print(response.content)
exit()
# Load the financial data into a pandas DataFrame
financial_data = pd.DataFrame(financial_data)
# Example expense ratio (expenses as a proportion of revenue)
expense_ratio = 0.6
# Example growth rate
growth_rate = 0.03
# Check if the data contains the column 'revenue'
if 'totalRevenue' not in financial_data.columns:
print("The data does not contain a column named 'totalRevenue'.")
if 'future_revenue' not in financial_data.columns:
print("The data does not contain a column named 'future_revenue'.")
else:
# Create a new column for future expenses by assuming a constant expense ratio
financial_data['future_expenses'] = financial_data['future_revenue'] * expense_ratio
else:
# Create a new column for future revenue by assuming a constant growth rate
financial_data['future_revenue'] = financial_data['totalRevenue'].iloc[-1] * (1 + growth_rate)**(range(1, len(financial_data) + 1))
# Create a new column for future expenses by assuming a constant expense ratio
financial_data['future_expenses'] = financial_data['future_revenue'] * expense_ratio
# Assume a discount rate of 10%
discount_rate = 0.1
如果沒有totalRevenue
和future_revenue
,您最終會進入一個未創建future_revenue
列的分支,並且您不能使用它來計算future_expenses
。
您程序的最后部分似乎簡化為
if "future_revenue" in financial_data.columns and "future_expenses" not in financial_data.columns:
financial_data["future_expenses"] = financial_data["future_revenue"] * expense_ratio
if "totalRevenue" in financial_data.columns:
financial_data["future_revenue"] = financial_data["totalRevenue"].iloc[-1] * (1 + growth_rate) ** (range(1, len(financial_data) + 1))
financial_data["future_expenses"] = financial_data["future_revenue"] * expense_ratio
但是您仍然需要一些方法來導出future_revenue
以便您可以計算future_expenses
; 現在如果沒有totalRevenue
就無法完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.