這是我的數據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')結果。也就是說,只有 threatened 列不顯示我實際上按 ie continent 和 threat_type 分組的列,盡管存在于我的數據框中。我想對我的數據框的大陸列執行操作,但它沒有顯示為列之一。例如 - continents = df.continent.unique()。此命令給我一個未找到的關鍵錯誤continent。
1 回答

MMTTMM
TA貢獻1869條經驗 獲得超4個贊
在 groupby...pandas 之后將 groupby 列放入索引中。在 pandas 中執行 groupby 后始終重置索引,不要這樣做drop=True
。
在你的代碼之后。
df = df.reset_index()
然后您將獲得所需的列。
添加回答
舉報
0/150
提交
取消