Reputation: 63
From a data frame I want to count how many rows have same value for each unique value in a column.
df = pd.DataFrame({'c':[1,1,2,2,3,3],'l1':['a','a','a','a','b','b'],'l3':['b','a','b','a','a','a'],'l4':[1,2,3,4,5,6]})
cols = ['c','l1']
out = (df.set_index(cols).assign(pair=df.groupby(cols)['l3']
.agg(makePair)).reset_index()
.reindex(df.columns.union(['pair'],sort=False),axis=1))
out['l1'].value_counts
I got wiered results as
<bound method IndexOpsMixin.value_counts of 0 a
1 a
2 a
3 a
4 b
5 b
Name: l1, dtype: object>
I expect
a 3
b 2
And actual ask is to do the value count on 'pair' ie out['pair'].value_counts
where the error was more complicated
Upvotes: 0
Views: 574
Reputation: 2443
You can use value_counts()
df = pd.DataFrame({'c':[1,1,2,2,3,3],'l1':['a','a','a','a','b','b'],'l3':['b','a','b','a','a','a'],'l4':[1,2,3,4,5,6]})
df['l1'].value_counts() # l1 is your column name
But, its already answered here. Next time google before asking!
Upvotes: 2