【问题标题】:How to add columns to multi-index dataframes?如何向多索引数据框添加列?
【发布时间】:2017-04-30 19:41:15
【问题描述】:

我有一个看起来像这样的多索引数据框:

       ACA FP Equity            UCG IM Equity            
          LAST PRICE     VOLUME    LAST PRICE      VOLUME
date                                                         
2010-01-04        12.825  5879617.0       15.0292  10844639.0
2010-01-05        13.020  6928587.0       14.8092  16456228.0
2010-01-06        13.250  5290631.0       14.6834  10446450.0
2010-01-07        13.255  5328586.0       15.0292  31900341.0
2010-01-08        13.470  7160295.0       15.1707  40750768.0

如果我想在数据框中为每个股票添加第三列,语法是什么?例如:

df['ACA FP Equity']['PriceVolume'] = df['ACA FP Equity']['LAST PRICE']*3

但我想为每个权益都这样做,而不是手动添加每个权益。

提前致谢。

【问题讨论】:

    标签: python python-2.7 python-3.x pandas dataframe


    【解决方案1】:

    如果您需要所有LAST PRICE 列乘以3,请使用slicers 选择它们并重命名列名称:

    idx = pd.IndexSlice
    df1 = df.loc[:, idx[:, 'LAST PRICE']].rename(columns={'LAST PRICE':'PriceVolume'}) * 3
    print (df1)
               ACA FP Equity UCG IM Equity
                 PriceVolume   PriceVolume
    2010-01-04        38.475       45.0876
    2010-01-05        39.060       44.4276
    2010-01-06        39.750       44.0502
    2010-01-07        39.765       45.0876
    2010-01-08        40.410       45.5121
    

    那么你需要concat输出:

    print (pd.concat([df,df1], axis=1))
               ACA FP Equity            UCG IM Equity             ACA FP Equity  \
                  LAST PRICE     VOLUME    LAST PRICE      VOLUME   PriceVolume   
    2010-01-04        12.825  5879617.0       15.0292  10844639.0        38.475   
    2010-01-05        13.020  6928587.0       14.8092  16456228.0        39.060   
    2010-01-06        13.250  5290631.0       14.6834  10446450.0        39.750   
    2010-01-07        13.255  5328586.0       15.0292  31900341.0        39.765   
    2010-01-08        13.470  7160295.0       15.1707  40750768.0        40.410   
    
               UCG IM Equity  
                 PriceVolume  
    2010-01-04       45.0876  
    2010-01-05       44.4276  
    2010-01-06       44.0502  
    2010-01-07       45.0876  
    2010-01-08       45.5121  
    

    没有concat 的另一种解决方案是从selected_df 的列创建元组,然后分配输出:

    idx = pd.IndexSlice
    selected_df = df.loc[:, idx[:, 'LAST PRICE']]
    
    new_cols = [(x, 'PriceVolume') for x in selected_df.columns.levels[0]]
    print (new_cols)
    [('ACA FP Equity', 'PriceVolume'), ('UCG IM Equity', 'PriceVolume')]
    
    df[new_cols] = selected_df * 3
    print(df)
               ACA FP Equity            UCG IM Equity             ACA FP Equity  \
                  LAST PRICE     VOLUME    LAST PRICE      VOLUME   PriceVolume   
    2010-01-04        12.825  5879617.0       15.0292  10844639.0        38.475   
    2010-01-05        13.020  6928587.0       14.8092  16456228.0        39.060   
    2010-01-06        13.250  5290631.0       14.6834  10446450.0        39.750   
    2010-01-07        13.255  5328586.0       15.0292  31900341.0        39.765   
    2010-01-08        13.470  7160295.0       15.1707  40750768.0        40.410   
    
               UCG IM Equity  
                 PriceVolume  
    2010-01-04       45.0876  
    2010-01-05       44.4276  
    2010-01-06       44.0502  
    2010-01-07       45.0876  
    2010-01-08       45.5121  
    

    【讨论】:

      【解决方案2】:

      我能想到的最优雅的方式是:

      df['ACA FP Equity']['PriceVolume'] = pd.Series(df['ACA FP Equity']['LAST PRICE'].apply(lambda x: x*3))
      

      apply 语句允许您执行给定函数,在本例中为 lambda expression,它将每个输入乘以三,用于数据框中指定列的每个值。运行 apply 语句将返回 pandas Series,然后可以将其作为列添加到数据框中。

      这是一个简单的示例,展示了它如何使用简单的数据框:

      import pandas as pd
      
      df = pd.DataFrame(data={'a': [1, 2, 3], 'b': [4, 5, 6]})
      print(df)
      
      # Output:
      # /  a  b
      # 0  1  4
      # 1  2  5
      # 2  3  6
      
      
      # Add column 'c'
      df['c'] = pd.Series(df['b'].apply(lambda x: x*3))
      print(df)
      
      # Output:
      # /  a  b  c
      # 0  1  4  12
      # 1  2  5  15
      # 2  3  6  18
      

      【讨论】:

        猜你喜欢
        • 2018-12-06
        • 2020-03-12
        • 2013-04-11
        • 2018-08-24
        • 2018-10-17
        • 2017-06-10
        • 2013-12-03
        • 1970-01-01
        • 2019-01-16
        相关资源
        最近更新 更多