python - Aggregate by count, keep all columns in Pandas -


here example pandas dataframe:

x = pd.dataframe({"id": [10, 10, 20, 10, 50, 50],                    "name": ["a", "a", "b", "a", "c", "c"]}) 

i show want do, using data.table in r:

x = data.table(id = c(10,10,20,10,50,50), name = c("a", "a", "b", "a", "c", "c")) x[, .n, = list(name, id)] 

which outputs:

   name id n  1:    10 3 2:    b 20 1 3:    c 50 2 

i can similar pandas, can't keep id column:

x["name"].value_counts() 

returns:

a    3 c    2 b    1 dtype: int64 

try length of each sub-group identified ['id', 'name'], , return group key index.

x.groupby(['id', 'name'], as_index=true).agg(len)  id  name 10        3 20  b       1 50  c       2 dtype: int64 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -