繁体   English   中英

在多索引 dataframe pandas 中添加两列

[英]adding two columns in multiindex dataframe pandas

我正在使用 pandas 1.14。

我有 dataframe 看起来像这样:

                          col1      col2  ....
    A   B   C  D  E    

   11   1   1  1  1        2          3
                  3        3          4
               30 3        10         2
                ...        ...
   22   3   4  5  6        3          1

df.index输出

MultiIndex([('11', '1', '1', '1', '1'),
            ('11', '1', '1', '1', '3'),
            ('11', '1', '1', '30', '3'),
            ...
            ('22', '3', '4', '5', '6')],
           names=["A","B","C", "D", "E"], length=10000)

df.columns输出

Index(["col1", "col2", ...], dtype="object")

我想做的是添加两列并除以 2。在单个索引 dataframe 我通常会做df["new"] = (df["col1"] + df["col2"])/2

如何使用多索引 dataframe 做到这一点?

我想要的 dataframe 应该是这样的

                          col1      col2  new
    A   B   C  D  E    

   11   1   1  1  1        2          3    2.5
                  3        3          4    3.5
               30 3        10         2    6
                ...        ...
   22   3   4  5  6        3          1    2

提前致谢!

您的解决方案也应该适用于 MultiIndexes

In [14]: df = pd.DataFrame([[2,3],[3,4],[10,2],[3,1]], columns=['col1', 'col2'], index=index)                                                                                                              

In [15]: df                                                                                                                                                                                                
Out[15]: 
             col1  col2
A  B C D  E            
11 1 1 1  1     2     3
          3     3     4
       30 3    10     2
22 3 4 5  6     3     1

In [16]: df['new'] = (df['col1'] + df['col2'])/2                                                                                                                                                           

In [17]: df                                                                                                                                                                                                
Out[17]: 
             col1  col2  new
A  B C D  E                 
11 1 1 1  1     2     3  2.5
          3     3     4  3.5
       30 3    10     2  6.0
22 3 4 5  6     3     1  2.0

我做了一个实验,你的方法应该有效。

df = pd.DataFrame({'a':[1,2,3,4], 'b':[2,3,4,5]}, index=[['1', '1', '2', '2'], ['1','2','1','2']])
df
>>>

     a  b
1 1  1  2
  2  2  3
2 1  3  4
  2  4  5

你的方法。

df['new'] = (df['a'] + df['b']) / 2

df
>>>
     a  b  new
1 1  1  2  1.5
  2  2  3  2.5
2 1  3  4  3.5
  2  4  5  4.5
```

无特殊处理,标准手法。 我的标准是始终使用assign()

df = pd.DataFrame({"A":[11],"B":[1],"C":[1],"D":[1],"E":[1],"col1":[2],"col2":[3]})
df = df.set_index(["A","B","C","D","E"])
df = df.assign(new=lambda dfa: dfa.sum(axis=1)/2)

print(df.to_string())

output

            col1  col2  new
A  B C D E                 
11 1 1 1 1     2     3  2.5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM