5

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

1 Answer 1

6

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.