答案 0 :(得分:1)
您需要特别注意如何指定axes
,但是您可以只使用.gt
+ .mean
+ .sum
import pandas as pd
import numpy as np
df = pd.DataFrame({'APPL': ['ACCMGR', 'ACCOUNTS', 'ADVISOR', 'AUTH', 'TEST'],
'Std_1': [106.875, 121.703, np.NaN, 116.8585, 1],
'Std_2': [130.1899, 113.4927, np.NaN, 112.4486, 4],
'Std_3': [107.186, 114.5418, np.NaN, 115.2699, np.NaN]})
df = df.set_index('APPL')
df['cts'] = df.gt(df.mean(axis=1), axis=0).sum(axis=1)
df = df.reset_index()
APPL Std_1 Std_2 Std_3 cts
0 ACCMGR 106.8750 130.1899 107.1860 1
1 ACCOUNTS 121.7030 113.4927 114.5418 1
2 ADVISOR NaN NaN NaN 0
3 AUTH 116.8585 112.4486 115.2699 2
4 TEST 1.0000 4.0000 NaN 1
答案 1 :(得分:0)
考虑的数据框
quantity price
0 6 1.45
1 3 1.85
2 2 2.25
在轴= 1上应用lambda函数,对于每个系列的行,检查值大于平均值的列并获取列的索引
df.apply(lambda x:df.columns.get_loc(x[x>np.mean(x)].index[0]),axis=1)
出局:
quantity price > than mean
0 6 1.45 0
1 3 1.85 0
2 2 2.25 1