Extraer valores en los recuentos de valores de Pandas()
Digamos que hemos usado pandas ' dataframe[column].value_counts()
que produce:
apple 5
sausage 2
banana 2
cheese 1
¿Cómo se extraen los valores de esto en el orden que se muestra arriba, por ejemplo, de máximo a mínimo ?
[apple,sausage,banana,cheese]
31
Author: JamesButterlips, 2016-02-20
4 answers
Prueba esto:
dataframe[column].value_counts().index.tolist()
['apple', 'sausage', 'banana', 'cheese']
46
Author: Mike Müller,
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
2016-02-20 13:19:08
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
2016-02-20 13:19:08
#!/usr/bin/env python
import pandas as pd
# Make example dataframe
df = pd.DataFrame([(1, 'Germany'),
(2, 'France'),
(3, 'Indonesia'),
(4, 'France'),
(5, 'France'),
(6, 'Germany'),
(7, 'UK'),
],
columns=['groupid', 'country'],
index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])
# What you're looking for
values = df['country'].value_counts().keys().tolist()
counts = df['country'].value_counts().tolist()
Ahora, print(df['country'].value_counts())
da:
France 3
Germany 2
UK 1
Indonesia 1
Y print(values)
da:
['France', 'Germany', 'UK', 'Indonesia']
Y print(counts)
da:
[3, 2, 1, 1]
11
Author: Martin Thoma,
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-05 14:16:54
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-05 14:16:54
Si alguien se lo perdió en los comentarios, pruebe esto:
dataframe[column].value_counts().to_frame()
5
Author: Sawant,
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-04-04 12:46:30
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-04-04 12:46:30
Primero tienes que sort
el dataframe
por la columna count
max
a min
si no está ordenada de esa manera ya. En tu post, ya está en el orden correcto pero lo haré sort
de todos modos:
dataframe.sort_index(by='count', ascending=[False])
col count
0 apple 5
1 sausage 2
2 banana 2
3 cheese 1
Luego puede enviar la columna col
a una lista:
dataframe['col'].tolist()
['apple', 'sausage', 'banana', 'cheese']
1
Author: Joe T. Boka,
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
2016-02-20 13:25: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
2016-02-20 13:25:37