这是我的数据
threats = pd.read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-08-18/threats.csv', index_col = 0)这是我的代码-
df = (threats
.query('threatened>0')
.groupby(['continent', 'threat_type'])
.agg({'threatened':'size'}))然而,只有df.columns Index(['threatened'], dtype='object')才是结果。也就是说,虽然存在于我的数据框中,但只有受威胁的列没有显示我实际按大陆和threat_type分组的列。
我想在我的数据框的大陆列上执行操作,但它没有显示为其中一列。用于eg - continents = df.continent.unique()。这个命令给我一个关键错误continent not found。
发布于 2020-08-23 19:43:46
在groupby...pandas将groupby列放入索引之后。在pandas中做groupby之后,总是重置索引,不要做drop=True。
在你的代码之后。
df = df.reset_index()然后,您将获得所需的列。
https://stackoverflow.com/questions/63546492
复制相似问题