這是我的資料框。我希望單獨計算每個開關的電流、電壓和功率的平均值。
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
如果有另一種不利用熊貓來幫助解決此問題的方法,我也愿意聽到
uj5u.com熱心網友回復:
另一種方法:
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
uj5u.com熱心網友回復:
嘗試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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/478284.html
