Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试图找出一种方法来匹配两个数据帧中的项_Python_Python 3.x_Dataframe - Fatal编程技术网

Python 试图找出一种方法来匹配两个数据帧中的项

Python 试图找出一种方法来匹配两个数据帧中的项,python,python-3.x,dataframe,Python,Python 3.x,Dataframe,我正在测试这段代码 # 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

我正在测试这段代码

# 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
            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
第二个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
            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
我想组合这些数据帧,以便最终结果如下所示

                                                 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
            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
。两者都不起作用。你知道我该怎么做吗?

只需创建一个字典
{ticker:ticker+name}
,然后转换列名:

d={'AAPL':'Apple Inc. AAPL'} #etc, simply split df1.str values by "-" to create this dict
df2.columns = [d.get(c) for c in df2.columns]

您可以使用字典而不是数据帧:

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

哇!这是一个很酷的把戏,大卫!我从来都不知道你能做到!谢谢你教我新东西!!