简体   繁体   中英

Pandas colnames not found after grouping and aggregating

Here is my data

threats = pd.read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-08-18/threats.csv', index_col = 0)

And here is my code -

df = (threats
.query('threatened>0')
.groupby(['continent', 'threat_type'])
.agg({'threatened':'size'}))

However df.columns only Index(['threatened'], dtype='object') is the result. That is, only the threatened column is displaying not the columns I have actually grouped by ie continent and threat_type although present in my data frame.

I would like to perform operation on the continent column of my data frame, but it is not displaying as one of the columns. For eg - continents = df.continent.unique() . This command gives me a key error of continent not found.

After groupby...pandas put the groupby columns in the index. Always reset index after doing groupby in pandas and don't do drop=True .

After your code.

df = df.reset_index()

And then you will get required columns.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM