在0级列上执行自定义计算。

huangapple go评论72阅读模式
英文:

Python Perform Custom Calculations on 0 level columns

问题

import pandas as pd

df = pd.DataFrame({
('A', 'a'): [1, 2, 3],
('A', 'b'): [4, 5, 6],
('B', 'a'): [7, 8, 9],
('B', 'b'): [10, 11, 12],
})
df

我想在每个Zero Level的每一列上执行一个定义好的计算,并输出另一个Zero Level标签 "C"。

我想执行 "A" * "B" / 2

得到一个数据帧的输出:

df = pd.DataFrame({
('A', 'a'): [1, 2, 3],
('A', 'b'): [4, 5, 6],
('B', 'a'): [7, 8, 9],
('B', 'b'): [10, 11, 12],
('C', 'a'): [3.5, 8, 13.5],
('C', 'b'): [20, 27.5, 36],
})
df

我的初始思路是在 level=0, axis=1 上进行 .groupby,然后使用 .apply() 函数。谢谢。

英文:

I have a Data Frame:

import pandas as pd

df = pd.DataFrame({
    ('A', 'a'): [1, 2, 3],
    ('A', 'b'): [4, 5, 6],
    ('B', 'a'): [7, 8, 9],
    ('B', 'b'): [10, 11, 12],
})
df

I would like to perform a defined calculation on each of the columns in each Zero Level and output another Zero Level label "C".

I would like to do "A" * "B" / 2

resulting in a Data Frame output:

df = pd.DataFrame({
    ('A', 'a'): [1, 2, 3],
    ('A', 'b'): [4, 5, 6],
    ('B', 'a'): [7, 8, 9],
    ('B', 'b'): [10, 11, 12],
    ('C', 'a'): [3.5, 8, 13.5],
    ('C', 'b'): [20, 27.5, 36],
})
df

My initial thought process was to do a .groupby on level=0, axis=1 then use .apply() with a function. Thanks.

答案1

得分: 2

你可以只执行你想要的计算 - 即 (df['A'] * df['B']).div(2) 并将这些值分配给原始DataFrame中的新列。

df[['C', 'a'], ['C', 'b']] = (df['A'] * df['B']).div(2)
   A  B    C     
   a  b   a   b   
0  1  4  7.0 10.0
1  2  5  8.0 11.0
2  3  6  9.0 12.0
英文:

You can just do the calculation you want - i.e, (df['A'] * df['B']).div(2) and assign the values to new columns in the original DataFrame.

df[[('C', 'a'), ('C', 'b')]] = (df['A'] * df['B']).div(2)

   A     B         C      
   a  b  a   b     a     b
0  1  4  7  10   3.5  20.0
1  2  5  8  11   8.0  27.5
2  3  6  9  12  13.5  36.0

答案2

得分: 2

使用 stack/unstackeval

out = df.stack().eval("C = A * B / 2").unstack()

输出:

print(out)

   A     B         C
   a  b  a   b     a     b
0  1  4  7  10   3.5  20.0
1  2  5  8  11   8.0  27.5
2  3  6  9  12  13.5  36.0
英文:

With stack/unstack and eval :

out = df.stack().eval("C = A * B / 2").unstack()

Output :

print(out)

   A     B         C
   a  b  a   b     a     b
0  1  4  7  10   3.5  20.0
1  2  5  8  11   8.0  27.5
2  3  6  9  12  13.5  36.0

答案3

得分: 0

以下是已翻译的代码部分:

pd.concat([df, pd.concat([df.groupby(level=1, axis=1).prod().div(2)], keys=['C'], axis=1)], axis=1)

输出结果:

       A     B         C      
       a  b  a   b     a     b
    0  1  4  7  10   3.5  20.0
    1  2  5  8  11   8.0  27.5
    2  3  6  9  12  13.5  36.0
英文:

Here is another way:

pd.concat([df,pd.concat([df.groupby(level=1,axis=1).prod().div(2)],keys =['C'],axis=1)],axis=1)

Output:

   A     B         C      
   a  b  a   b     a     b
0  1  4  7  10   3.5  20.0
1  2  5  8  11   8.0  27.5
2  3  6  9  12  13.5  36.0

huangapple
  • 本文由 发表于 2023年3月10日 01:29:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定