簡體   English   中英

通過擴展列 Pandas 中的值來分組並計數

[英]Group and take count by expanding values in column Pandas

我希望分組,然后使用特定列中的值創建列標題並列出它們的計數。

數據

location    box     type    
ny          box11   hey 
ny          box11   hey 
ny          box13   hello   
ny          box13   hello   
ny          box13   hello   
ca          box5    hi  
ca          box8    hello


        

想要的

location    hey hello   hi
ny          2   3       0
ca          0   1       1

正在做

使用交叉表作為 SO 成員協助此腳本。

第一組,然后是交叉表

df1 = df.groupby(["location", "box"]).agg()

df2 = pd.crosstab([df["location"], df["box"]], df["type"])

任何建議表示贊賞 - 仍在研究

不需要box

df1 = pd.crosstab(df["location"], df["type"])
Out[271]: 
type      hello  hey  hi
location                
ca            1    0   1
ny            3    2   0

暫無
暫無

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

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