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

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

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:

  1. import pandas as pd
  2. df = pd.DataFrame({
  3. ('A', 'a'): [1, 2, 3],
  4. ('A', 'b'): [4, 5, 6],
  5. ('B', 'a'): [7, 8, 9],
  6. ('B', 'b'): [10, 11, 12],
  7. })
  8. 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:

  1. df = pd.DataFrame({
  2. ('A', 'a'): [1, 2, 3],
  3. ('A', 'b'): [4, 5, 6],
  4. ('B', 'a'): [7, 8, 9],
  5. ('B', 'b'): [10, 11, 12],
  6. ('C', 'a'): [3.5, 8, 13.5],
  7. ('C', 'b'): [20, 27.5, 36],
  8. })
  9. 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中的新列。

  1. df[['C', 'a'], ['C', 'b']] = (df['A'] * df['B']).div(2)
  1. A B C
  2. a b a b
  3. 0 1 4 7.0 10.0
  4. 1 2 5 8.0 11.0
  5. 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.

  1. df[[('C', 'a'), ('C', 'b')]] = (df['A'] * df['B']).div(2)
  2. A B C
  3. a b a b a b
  4. 0 1 4 7 10 3.5 20.0
  5. 1 2 5 8 11 8.0 27.5
  6. 2 3 6 9 12 13.5 36.0

答案2

得分: 2

使用 stack/unstackeval

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

输出:

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

With stack/unstack and eval :

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

Output :

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

答案3

得分: 0

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

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

输出结果:

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

Here is another way:

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

Output:

  1. A B C
  2. a b a b a b
  3. 0 1 4 7 10 3.5 20.0
  4. 1 2 5 8 11 8.0 27.5
  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:

确定