I am stuck with this. I would like to get a list of name
from the following, a result of groupby().mean()
with the application of pandas DataFrame. Most specifically, I would like to get ["John", "Mary", "Suzan", "Eric"]
.
score
name
John 85.0
Mary 86.5
Suzan 90.0
Eric 100.0
The result of the above is means
, which comes from the following:
data = pandas.DataFrame({"name": names, "score": scores})
means = data.groupby("name").mean()
As now I have means
, I would like to get a list of names from 'means' - ["John", "Mary", "Suzan", "Eric"]
. Is this achievable?
Just look at the index if you have an Index. If you have a MultiIndex, see @jezrael's answer with get_level_values
.
means.index.tolist()
You can use:
print (list(means.index))
['John', 'Mary', 'Suzan', 'Eric']
Another better solution is use Series.unique
and omit groupby
:
print (data.name.unique())
['John' 'Mary' 'Suzan' 'Eric']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.