Python Pandas agregar columna para el valor máximo de las columnas seleccionadas
data = {'name' : ['bill', 'joe', 'steve'],
'test1' : [85, 75, 85],
'test2' : [35, 45, 83],
'test3' : [51, 61, 45]}
frame = pd.DataFrame(data)
Me gustaría agregar una nueva columna que muestre el valor máximo para cada fila.
Salida deseada:
name test1 test2 test3 HighScore
bill 75 75 85 85
joe 35 45 83 83
steve 51 61 45 61
A Veces
frame['HighScore'] = max(data['test1'], data['test2'], data['test3'])
Funciona, pero la mayoría de las veces da este error:
ValueError: El valor de verdad de un array con más de un elemento es ambiguo. Usar un. any () o un. all()
¿Por qué solo funciona a veces? Hay otra forma de hacerlo?
57
3 answers
>>> frame['HighScore'] = frame[['test1','test2','test3']].max(axis=1)
>>> frame
name test1 test2 test3 HighScore
0 bill 85 35 51 85
1 joe 75 45 61 75
2 steve 85 83 45 85
90
Author: Roman Pekar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-17 16:33:37
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-17 16:33:37
>>> frame['HighScore'] = frame[['test1','test2','test3']].apply(max, axis=1)
>>> frame
name test1 test2 test3 HighScore
0 bill 85 35 51 85
1 joe 75 45 61 75
2 steve 85 83 45 85
12
Author: alko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-17 16:43:52
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-17 16:43:52
Si se debe determinar un valor max
o min
entre varias columnas en un df
, utilice:
df['Z']=df[['A','B','C']].apply(np.max,axis=1)
0
Author: Vikas goel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-18 06:12:19
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-18 06:12:19