【发布时间】:2021-10-13 04:15:15
【问题描述】:
我有两个数据框,第一个代表股价,第二个代表公司名称。当我尝试将它们合并到一个唯一键上时,即 SimFinId,一切都很好,只是我丢失了股价表中的 Ticker 和 Date 列。我需要在每次合并时保留这两列 - 我稍后还希望将输出与第三个表合并 - 这样我以后可以在日期之后过滤。我正在使用 Jupyter。任何帮助将不胜感激!
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')
【问题讨论】:
标签: python pandas dataframe merge