Поскольку приведено очень мало кода, я предоставил решение, которое создает столбцы greater
, less
и count
.Кроме того, в вопросе не указано, что делать с условием, когда значение равно среднему значению, поэтому я не кодировал это условие.
headers = ['APPL', 'Std_1', 'Std_2', 'Std_3', 'Mean']
values = [['ACCMGR', 106.8754, 130.1600, 107.1861, 114.750510],
['ACCOUNTS', 121.7034, 113.4927, 114.5482, 116.581458],
['AUTH', 116.8585, 112.4487, 115.2700, 114.859050]]
df = pd.DataFrame(values, columns=headers)
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 5 columns):
APPL 3 non-null object
Std_1 3 non-null float64
Std_2 3 non-null float64
Std_3 3 non-null float64
Mean 3 non-null float64
dtypes: float64(4), object(1)
memory usage: 200.0+ bytes
def make_count(comp_cols, mean_col):
count_d = {'greater': 0, 'less': 0}
for col in comp_cols:
if col > mean_col:
count_d['greater'] += 1
elif col < mean_col:
count_d['less'] += 1
return count_d['greater'], count_d['less'], (count_d['greater'] + count_d['less'])
def apply_make_count(df):
a, b, c = df.apply(lambda row: make_count([row['Std_1'], row['Std_2'], row['Std_3']], row['Mean']), axis=1)
df['greater'], df['less'], df['count'] = list(zip(a, b, c))
apply_make_count(df)
df =
APPL Std_1 Std_2 Std_3 Mean greater less count
0 ACCMGR 106.8754 130.1600 107.1861 114.750510 1 2 3
1 ACCOUNTS 121.7034 113.4927 114.5482 116.581458 1 2 3
2 AUTH 116.8585 112.4487 115.2700 114.859050 2 1 3