簡體   English   中英

pandas.DataFrame.groupby.nunique()不會刪除groupby列。 這是錯誤嗎?

[英]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.

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