我有df3:
1 2 3
A Battery Voltage, V (Avg) 12 13 14
A Max Watt, W 4 5 10
A Battery Temperature, C 20 21 22
我正在尝试添加新列:
M = ['Battery Voltage, V (Avg)', 'Battery Temperature, C']
df3['Total/Max/Min'] = np.where(mask3, df3.mean(axis=1), df3.sum(axis=1))
但我对“均值”值
感到空白 1 2 3 Total/Max/Min
A Battery Voltage, V (Avg) 12 13 14 <--
A Max Watt, W 4 5 10 19
A Battery Temperature, C 20 21 22 <--
编辑:
这两行的type
为string
,即使这些值都是数字。我似乎无法转换为float()
EDIT2: 实际df:
1 2 3 4
CU0111-023349-J Output Energy, (Wh/h) 3.825 1.46 1.57 1.997
CU0111-023349-J Lights (Wh) 1.53 1.46 1.57 1.997
CU0111-023349-J Lights+Media (Wh) 2.295 0 0 0
CU0111-023349-J Total Usage (h) 1 1 1 1
CU0111-023349-J Lights (h) 0.4 1 1 1
CU0111-023349-J Light+Media (h) 0.6 0 0 0
CU0111-023349-J Battery Voltage, V (Max) 12.525 12.51 12.502 12.479
CU0111-023349-J Battery Voltage, V (Min) 12.44 12.467 12.421 12.409
CU0111-023349-J Battery Voltage, V (Avg) 12.463 12.494 12.484 12.454
CU0111-023349-J Max Watt, W 5.213 2.784 3.723 3.615
CU0111-023349-J Battery Temperature, C 19.555 18.977 18.324 17.753
CU0111-023349-J Charge Energy, Wh/h 0.016 0.006 0.009 0.01
CU0111-023349-J Time of Full Charge 11:21
实际代码:
L = ['Battery Voltage, V (Max)','Max Watt, W']
M = ['Battery Voltage, V (Avg)','Battery Temperature, C']
mask1 = df3.index.get_level_values(1).isin(L)
mask3 = df3.index.get_level_values(1).isin(M)
mask2 = df3.index.get_level_values(1) == 'Battery Voltage, V (Min)'
df3['Total/Max/Min'] = np.where(mask1, df3.max(axis=1),np.where(mask2, df3.min(axis=1),np.where(mask3, df3.median(axis=1), df3.sum(axis=1))))
result:
答案 0 :(得分:2)
更新:将所有列转换为数字dtypes:
df3 = df3.apply(pd.to_numeric, errors='coerce')
我无法重现您的问题:
In [182]: df3
Out[182]:
1 2 3
A Battery Voltage, V (Avg) 12 13 14
Max Watt, W 4 5 10
Battery Temperature, C 20 21 22
In [183]: mask3
Out[183]: [True, True, False]
In [184]: df3['Total/Max/Min'] = np.where(mask3, df3.mean(axis=1), df3.sum(axis=1))
In [185]: df3
Out[185]:
1 2 3 Total/Max/Min
A Battery Voltage, V (Avg) 12 13 14 13.000000
Max Watt, W 4 5 10 6.333333
Battery Temperature, C 20 21 22 63.000000
您能提供可重复的数据集吗?