I think this is a fairly basic question, but I can't seem to find the solution.
I have a pandas dataframe similar to the following:
import pandas as pd
df = pd.DataFrame({'A' : ['x','x','y','z','z'],
'B' : ['p','p','q','r','r']})
df
which creates a table like this:
A B
0 x p
1 x p
2 y q
3 z r
4 z r
I'm trying to create a table that represents the number of distinct values in that dataframe. So my goal is something like this:
A B c
0 x p 2
1 y q 1
2 z r 2
I can't find the correct functions to achieve this, though. I've tried:
df.groupby(['A','B']).agg('count')
This produces a table with 3 rows (as expected) but without a 'count' column. I don't know how to add in that count column. Could someone point me in the right direction?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…