簡體   English   中英

分組和聚合后找不到 Pandas colnames

[英]Pandas colnames not found after grouping and aggregating

這是我的數據

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')是結果。 也就是說,盡管存在於我的數據框中,但只有受威脅的列不顯示我實際按 ie 大洲和威脅類型分組的列。

我想對我的數據框的大洲列執行操作,但它沒有顯示為列之一。 例如 - continents = df.continent.unique() 這個命令給了我一個找不到continent的關鍵錯誤。

在 groupby...pandas 之后將 groupby 列放在索引中。 在 Pandas 中執行 groupby 后始終重置索引,不要執行drop=True

在你的代碼之后。

df = df.reset_index()

然后您將獲得所需的列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM