我有两个数据帧,第一个表示股价,第二个表示公司名称。当我尝试将它们合并到一个唯一的键(即SimFinId)上时,除了股票价格表中的Ticker和Date列丢失之外,一切都很好。我需要在每次合并时保留这两列-我还希望稍后将输出与第三个表合并-以便以后可以在日期之后进行过滤。我用木星。任何帮助将非常感激!
pip install simfin
# Import the main functionality from the SimFin Python API.
import simfin as sf
# Import names used for easy access to SimFin's data-columns.
from simfin.names import *
import datetime
# Set your API-key for downloading data. This key gets the free data.
sf.set_api_key('free')
# Set the local directory where data-files are stored.
# The directory will be created if it does not already exist.
sf.set_data_dir('~/simfin_data/')
# Download the data from the SimFin server and load into a Pandas DataFrame.
df_prices = sf.load_shareprices(variant='daily', market='us', index=[TICKER, DATE])
这是股票价格表
SimFinId Open Low High Close Adj. Close Dividend
Ticker Date
A 2007-01-03 45846 34.99 34.05 35.48 34.30 22.66 NaN
2007-01-04 45846 34.30 33.46 34.60 34.41 22.73 NaN
2007-01-05 45846 34.30 34.00 34.40 34.09 22.52 NaN
2007-01-08 45846 33.98 33.68 34.08 33.97 22.44 NaN
2007-01-09 45846 34.08 33.63 34.32 34.01 22.47 NaN
Volume Shares Outstanding
Ticker Date
A 2007-01-03 2574600 NaN
2007-01-04 2073700 NaN
2007-01-05 2676600 NaN
2007-01-08 1557200 NaN
2007-01-09 1386200 NaN
# Download the companies data from the SimFin server and load into a Pandas DataFrame.
df_companies = sf.load_companies(market='us', index=[TICKER])
这是公司名称表
SimFinId Company Name IndustryId
Ticker
A 45846 AGILENT TECHNOLOGIES INC 106001.0
AA 367153 Alcoa Corp 110004.0
AAC_delist 939324 AAC Holdings, Inc. 106011.0
AAL 68568 American Airlines Group Inc. 100006.0
AAMC 847094 Altisource Asset Management Corp 104001.0
# Merging the share prices Data with the companies Data on SimFinId.
pd.merge(df_companies, df_prices, on='SimFinId')
该图像显示了合并的输出
我认为,我在这里错过的是,我必须在合并语句中的每个数据帧之后添加reset.index()。为了以防万一,有人遇到了同样简单的问题。