[英]pandas.DataFrame.groupby.nunique() does not drop the groupby column/s. Is this a bug?
盡管我將參數as_index設置為True,但pandas.DataFrame.groupby.nunique()會將我要分組的列保留在結果中。
熊貓版本是:0.24.1
df = pd.DataFrame(
{'a': [1, 1, 2, 3, 2],
'b': [1, 2, 3, 4, 4]}
)
df.groupby('a', as_index=True).nunique()
輸出為:
# a b
# a
# 1 1 2
# 2 1 2
# 3 1 1
我期望:
# b
# a
# 1 2
# 2 2
# 3 1
作為反例,其行為符合預期:
df.groupby('a', as_index=True).max()
結果是:
# b
# a
# 1 2
# 2 4
# 3 4
如果對[print(df.to_string() + '\\n') for i, df in df.groupby('a', as_index=True)]
運行[print(df.to_string() + '\\n') for i, df in df.groupby('a', as_index=True)]
:
a b
0 1 1
1 1 2
a b
2 2 3
4 2 4
a b
3 3 4
沒有將a
列設置為每個數據框組的索引。 它是一種具有其索引設置為組索引的GROUPBY的輸出時as_index=True
(其也是默認值),而不是數據幀組本身。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.