python - 试图找出一种方法来匹配两个数据框中的项目
问题描述
我正在测试这段代码。
# Define the ticker list
from pandas import DataFrame
import pandas as pd
tickers_list = ['AAPL', 'WMT', 'IBM', 'MU', 'BA', 'AXP']
# Fetch the data
import yfinance as yf
company_names = []
for item in tickers_list:
my_stock = yf.Ticker(item)
company_name = my_stock.info['longName']
company_names.append(item + '-' + company_name)
df_names = DataFrame(company_names)
print(df_names)
pd.set_option('display.max_columns', None)
data_hist = yf.download(tickers_list,'2020-05-15')['Adj Close']
print(data_hist.head(5))
第一个 DF 看起来像这样。
0
0 AAPL-Apple Inc.
1 WMT-Walmart Inc.
2 IBM-International Business Machines Corporation
3 MU-Micron Technology, Inc.
4 BA-The Boeing Company
5 AXP-American Express Company
第二个 DF 看起来像这样。
AAPL AXP BA IBM MU WMT
Date
5/15/2020 307.709991 82.220001 120 116.980003 44.41 125.940002
5/18/2020 314.959991 88.410004 135.440002 121.559998 45.919998 127.660004
5/19/2020 313.140015 87.260002 130.440002 120.290001 45.119999 124.949997
5/20/2020 319.230011 90.730003 133.320007 121.379997 46.639999 125.449997
5/21/2020 316.850006 89.830002 139 119.120003 45.220001 124.989998
我想组合这些数据框,所以最终结果看起来像这样。
Apple Inc. American Express Company The Boeing Company International Business Machines Corporation Micron Technology, Inc. Walmart Inc.
AAPL AXP BA IBM MU WMT
Date
5/15/2020 307.709991 82.220001 120 116.980003 44.41 125.940002
5/18/2020 314.959991 88.410004 135.440002 121.559998 45.919998 127.660004
5/19/2020 313.140015 87.260002 130.440002 120.290001 45.119999 124.949997
5/20/2020 319.230011 90.730003 133.320007 121.379997 46.639999 125.449997
5/21/2020 316.850006 89.830002 139 119.120003 45.220001 124.989998
我认为这将需要某种查找,因为我无法根据序数位置匹配这些代码,因为它'yf.download'
似乎在运行时对字段名称进行了字母排序。我试过concat
了merge
。都没有奏效。知道如何让这个工作吗?
解决方案
您可以使用字典而不是数据框:
d = {}
for item in tickers_list:
my_stock = yf.Ticker(item)
company_name = my_stock.info['longName']
d[item] = company_name
print(d)
{'AAPL': 'Apple Inc.',
'WMT': 'Walmart Inc.',
'IBM': 'International Business Machines Corporation',
'MU': 'Micron Technology, Inc.',
'BA': 'The Boeing Company',
'AXP': 'American Express Company'}
然后使用以下命令更新第二个数据框中的列map
:
df2.columns = df2.columns.map(d)+" - "+df2.columns
输出:
Apple Inc. - AAPL American Express Company - AXP The Boeing Company - BA International Business Machines Corporation - IBM Micron Technology, Inc. - MU Walmart Inc. - WMT
Date
5/15/2020 307.709991 82.220001 120.000000 116.980003 44.410000 125.940002
5/18/2020 314.959991 88.410004 135.440002 121.559998 45.919998 127.660004
5/19/2020 313.140015 87.260002 130.440002 120.290001 45.119999 124.949997
5/20/2020 319.230011 90.730003 133.320007 121.379997 46.639999 125.449997
5/21/2020 316.850006 89.830002 139.000000 119.120003 45.220001 124.989998
推荐阅读
- spring-boot - 带有参数的thymeleaf 3表单操作URL和get方法不起作用
- arrays - 当函数/过程需要使用接口类型的数组时,传递使用实现接口的类类型的数组?
- javascript - Cloud Functions 在超时后继续执行
- types - 什么比喻最能描述“价值”和“类型”之间的关系?
- c# - 替换部分字符串 C#
- db2 - DB2 如何将下面的“with temp as”转换为普通的select语句
- r - 获取 R Shiny 中异步期货子进程的 PID
- selenium - Blazemeter 中 Taurus 的文件路径问题
- swift - 同一地址的内存安全:Int vs. Tuplet vs. Struct 冲突
- python - 从网络控制机器人