Я получаю данные индекса S&P за каждый день и хочу вставить цену закрытия и дату в свою базу данных, однако я новичок в Python и понятия не имею, как ориентироваться в этой странной структуре данных. Вот что возвращает print(spxHistoricalData):
Price Close High Low Open Volume
Ticker ^GSPC ^GSPC ^GSPC ^GSPC ^GSPC
Date
2000-01-03 1455.219971 1478.000000 1438.359985 1469.250000 931800000
2000-01-04 1399.420044 1455.219971 1397.430054 1455.219971 1009000000
2000-01-05 1402.109985 1413.270020 1377.680054 1399.420044 1085500000
2000-01-06 1403.449951 1411.900024 1392.099976 1402.109985 1092300000
2000-01-07 1441.469971 1441.469971 1400.729980 1403.449951 1225200000
Теперь я попробовал сделать это:
print(spxHistoricalData[["Date", "Close"]])
Но я просто получаю сообщение об ошибке KeyError: «['Date'] нет в индексе»
В настоящее время мой код выглядит так :
import yfinance as yf
import mysql.connector
import pandas as pd
spxHistoricalData = yf.download("^GSPC", start="2000-01-01", end="2025-01-01")
print(spxHistoricalData)
print(spxHistoricalData[["Date", "Close"]])
connection = mysql.connector.connect(
host="127.0.0.1",
port=3306,
user="root",
password="password",
database="bitcoin_and_sp500_price_prediction"
)
cursor = connection.cursor()
values = [(row["Date"], row["Close"]) for index, row in spxHistoricalData.iterrows()]
print(values)
# Use executemany for batch insertion
try:
sql_query = "INSERT INTO spx_historical_data (date, close_price) VALUES (%s, %s)"
cursor.executemany(sql_query, values)
connection.commit()
print("All data inserted successfully using batch insertion.")
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
cursor.close()
connection.close()
Подробнее здесь: https://stackoverflow.com/questions/793 ... returned-b
Как мне получить только цену закрытия и дату из этих данных, возвращенных yfinance? ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение