Andreas
Andreas

Reputation: 53

Frequency count of values in a column of a pandas DataFrame

Please help me find a solution for this: I have a Pandas DataFrame containing website visitors and date of their visit. Now I want to know, how many people visit once, twice, etc.

I start with a table:

Visitor |   Date
---------------------
   A    |    Jan-1st
   B    |    Jan-1st
   C    |    Jan-2nd
   D    |    Jan-2nd
   A    |    Jan-2nd

I want to end up with a result in the form of:

Frequency |  No. of
of visits |  visitors
-----------------------
   1      |      3
   2      |      1

Upvotes: 5

Views: 3374

Answers (1)

Zero
Zero

Reputation: 77027

Usevalue_count on Visitor column twice.

In [182]: df.Visitor.value_counts().value_counts()
Out[182]:
1    3
2    1

Details

First get, visitor-wise visits, then you get group the similar counts.

In [183]: df.Visitor.value_counts()
Out[183]:
A    2
D    1
B    1
C    1
Name: Visitor, dtype: int64

In [188]: (df.Visitor.value_counts()
             .value_counts()
             .reset_index()
             .rename(columns={'index': 'Freq of visits', 'Visitor': 'No. of visitors'}))
Out[188]:
   Freq of visits  No. of visitors
0               1                3
1               2                1

Upvotes: 6

Related Questions