这是我的dataframe。我希望单独计算每个开关的电流、电压和功率的 average。
switch12 switch11 switch10
Current [0.1, 0.12, 0.15] [0.15, 0.11, 0.13] [0.18, 0.17, 0.15]
Voltage [120, 118] [120, 116] [120, 117]
Power [12, 25] [18, 19] [21.6, 22.4]
希望输出是这样的:
switch12 switch11 switch10
Current 0.123 0.130 0.167
Voltage 119 118 118.5
Power 18.5 18.5 22.0
如果有另一种方法不使用 pandas 来帮助解决这个问题,我也愿意听到
回答1
另一种方法:
out = df.explode([*df.columns]).groupby(level=0).mean()
print(out)
印刷:
switch12 switch11 switch10
Current 0.123333 0.13 0.166667
Power 18.500000 18.50 22.000000
Voltage 119.000000 118.00 118.500000
回答2
试试 applymap
df = df.applymap(np.mean)
# or
df = df.applymap(lambda l: sum(l) / len(l))
print(df)
switch12 switch11 switch13
0 0.123333 0.13 0.166667
1 119.000000 118.00 118.500000
2 18.500000 18.50 22.000000