[英]Pandas drop group column after groupby.apply(..)
uid iid val
uid
1 1 1 5 5.5
2 3 1 4 3.5
2 2 1 4 3.5
2 7 1 4 3.5
2 9 1 4 3.5
2 11 1 4 3.5
從上面的數據框中,我想刪除第一列,即:
uid
1
2
2
2
2
2
並提取
uid iid val
1 1 5 5.5
3 1 4 3.5
2 1 4 3.5
7 1 4 3.5
9 1 4 3.5
11 1 4 3.5
有人可以幫忙嗎?
您可以通過將group_keys=False
傳遞給groupby
來避免首先在索引中包含uid
df.groupby('uid', group_keys=False).apply(lambda x: x.tail(len(x) // 5))
uid iid val
4 1 5 5.5
使用reset_index
或droplevel
:
df = df.reset_index(level=0, drop=True)
df = df.reset_index(level='uid', drop=True)
要么:
df.index = df.index.droplevel(0)
您可以將as_index
設置為False
以從df中分組索引。
df.groupby('uid', as_index=False)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.