【问题标题】:How to concatenate a dataframe to a multiindex main dataframe along columns如何沿列将数据框连接到多索引主数据框
【发布时间】:2022-01-04 06:39:06
【问题描述】:

我已经尝试了一些答案,但在我的情况下无法获得所需的结果。

我正在处理股票数据。 我有一个列表['3MINDIA.NS.csv', 'AARTIDRUGS.NS.csv', 'AARTIIND.NS.csv', 'AAVAS.NS.csv', 'ABB.NS.csv'] 对于列表中的每只股票,我都会得到一个包含交易和相关信息的输出。它看起来像这样:

               BUY         SELL         profits    rel_profits
0           2004-01-13  2004-01-27   -44.200012   -0.094606
1           2004-02-05  2004-02-16    18.000000    0.044776
2           2005-03-08  2005-03-11    25.000000    0.048077
3           2005-03-31  2005-04-01    13.000000    0.025641
4           2005-10-11  2005-10-26   -20.400024   -0.025342
5           2005-10-31  2005-11-04    67.000000    0.095578
6           2006-05-22  2006-06-05   -55.100098   -0.046693
7           2007-03-06  2007-03-14     3.000000    0.001884
8           2007-03-19  2007-03-28    41.500000    0.028222
9           2007-07-31  2007-08-14    69.949951    0.038224
10          2008-01-24  2008-02-05    25.000000    0.013055
11          2009-11-04  2009-11-05    50.000000    0.031250
12          2010-12-10  2010-12-15    63.949951    0.018612
13          2011-02-02  2011-02-15   -53.050049   -0.015543
14          2011-09-30  2011-10-07    74.799805    0.018181
15          2015-12-09  2015-12-18  -215.049805   -0.019523
16          2016-01-18  2016-02-01  -475.000000   -0.046005
17          2016-11-16  2016-11-30 -1217.500000   -0.096877
18          2018-03-26  2018-04-02     0.250000    0.000013
19          2018-05-22  2018-05-25   250.000000    0.012626
20          2018-06-05  2018-06-12   101.849609    0.005361
21          2018-09-25  2018-10-10 -2150.000000   -0.090717
22          2021-01-27  2021-02-03   500.150391    0.024638
23          2021-06-30  2021-07-07   393.000000    0.016038
24          2021-08-12  2021-08-13   840.000000    0.035279
25                 NaN         NaN -1693.850281    0.995277

# note: every dataframe will have a last row with NaN values in buy, sell columns
# each datafram has different number of rows

现在我尝试向这个数据框添加额外级别的索引,如下所示:

symbol = name of the stock from given list for ex. for 3MINDIA.NS.csv symbol is 3MINDIA

trades.columns = pd.MultiIndex.from_product([[symbol], trades.columns])
 

在此之后,我尝试使用以下方法将循环中生成的每个 trades 数据帧连接到主数据帧:

result_df = pd.concat([result_df, trades], axis=1)

# I am trying to do this so that Whenever 
I call result_df[symbol] I should be able 
to see the trade dates for that particular symbol.

但是我得到一个包含很多 NaN 值的 result_df,因为每个 trades 数据帧中的行数都是可变的。

有什么方法可以将trades 数据框与股票代码列组合为更高级别的索引,而不是在我的result_df 中获取所有NaN

result_df我知道了

【问题讨论】:

    标签: dataframe concatenation multi-index


    【解决方案1】:

    所以我找到了一种方法来获得我想要的东西。 首先我在循环中添加了这段代码

    trades = pd.concat([trades], keys=[symbol], names=['Stocks'])
    
    

    在此之后,我在 result_dftrades 上再次使用了连接

    # Desired Result
    result_df = pd.concat([result_df, trades], axis=0, ignore_index=False)
    
    

    还有 BAM!!!这正是我想要的

    【讨论】:

      猜你喜欢
      • 2019-10-13
      • 2021-02-07
      • 1970-01-01
      • 2019-04-05
      • 2020-05-14
      • 2019-01-16
      • 1970-01-01
      • 2017-09-11
      • 2020-12-04
      相关资源
      最近更新 更多