Pandas: Count Unique Values after Resample

Malcolm Bastien

I'm just getting started with Pandas and am trying to combine: Grouping my data by date, and counting the unique values in each group.

Here's what my data looks like:

                  User, Type
Datetime
2014-04-15 11:00:00, A, New
2014-04-15 12:00:00, B, Returning
2014-04-15 13:00:00, C, New
2014-04-20 14:00:00, D, New
2014-04-20 15:00:00, B, Returning
2014-04-20 16:00:00, B, Returning
2014-04-20 17:00:00, D, Returning

And here's what I would like to get to: Resample the datetime index to the day (which I can do), and also count the unique users for each day. I'm not interested in the 'Type' column yet.

Day, Unique Users
2014-04-15, 3
2014-04-20, 2

I'm trying df.user.resample('D', how='count').unique but it doesn't seem to give me the right answer.

Karl D.

You don't need to do a resample to get the desired output in your question. I think you can get by with just a groupby on date:

print df.groupby(df.index.date)['User'].nunique()

2014-04-15    3
2014-04-20    2
dtype: int64

And then if you want to you could resample to fill in the time series gaps after you count the unique users:

cnt = df.groupby(df.index.date)['User'].nunique()
cnt.index = cnt.index.to_datetime()
print cnt.resample('D')

2014-04-15     3
2014-04-16   NaN
2014-04-17   NaN
2014-04-18   NaN
2014-04-19   NaN
2014-04-20     2
Freq: D, dtype: float64

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cumulative count of unique values in pandas

From Dev

pandas NaN after resample

From Dev

python pandas resample count and sum

From Dev

Count the unique values after array Join in Presto

From Java

Count unique values with pandas per groups

From Dev

Get count unique values in a row in pandas

From Java

Count of current unique values in a pandas df

From Java

Count unique values using pandas groupby

From Dev

How to count unique values in a dictionary of lists with Pandas?

From Dev

Count unique values of a series based on condition - Pandas

From Dev

Groupby and count the number of unique values (Pandas)

From Dev

Pivot table in pandas to count unique values

From Dev

pandas count unique values considering column

From Dev

Pandas Dataframes Remove rows by unique count of values

From Dev

Count unique values in csv column without pandas

From Dev

Pandas Dataframe resample on ms values

From Dev

Resample after rolling mean in pandas

From Dev

How to count unique values in pandas column base on dictionary values

From Dev

How to iterate over unique values in pandas and count frequency of associated values

From Dev

Count the values after an underscore in a Pandas Series

From Dev

sub count of column values after group by pandas

From Dev

Pandas groupby multiple columns, count, and resample

From Dev

Pandas dataframe resample and count events per day

From Dev

Pandas datetime resample count non-zero

From Dev

Pandas resample by day and count occurrences to new column

From Dev

How can I count the unique values in a Pandas Dataframe?

From Java

unique combinations of values in selected columns in pandas data frame and count

From Dev

one year rolling count of unique values by group in pandas

From Dev

Is there a way to use the unique values of a count of occurences into column headers pandas?

Related Related

  1. 1

    Cumulative count of unique values in pandas

  2. 2

    pandas NaN after resample

  3. 3

    python pandas resample count and sum

  4. 4

    Count the unique values after array Join in Presto

  5. 5

    Count unique values with pandas per groups

  6. 6

    Get count unique values in a row in pandas

  7. 7

    Count of current unique values in a pandas df

  8. 8

    Count unique values using pandas groupby

  9. 9

    How to count unique values in a dictionary of lists with Pandas?

  10. 10

    Count unique values of a series based on condition - Pandas

  11. 11

    Groupby and count the number of unique values (Pandas)

  12. 12

    Pivot table in pandas to count unique values

  13. 13

    pandas count unique values considering column

  14. 14

    Pandas Dataframes Remove rows by unique count of values

  15. 15

    Count unique values in csv column without pandas

  16. 16

    Pandas Dataframe resample on ms values

  17. 17

    Resample after rolling mean in pandas

  18. 18

    How to count unique values in pandas column base on dictionary values

  19. 19

    How to iterate over unique values in pandas and count frequency of associated values

  20. 20

    Count the values after an underscore in a Pandas Series

  21. 21

    sub count of column values after group by pandas

  22. 22

    Pandas groupby multiple columns, count, and resample

  23. 23

    Pandas dataframe resample and count events per day

  24. 24

    Pandas datetime resample count non-zero

  25. 25

    Pandas resample by day and count occurrences to new column

  26. 26

    How can I count the unique values in a Pandas Dataframe?

  27. 27

    unique combinations of values in selected columns in pandas data frame and count

  28. 28

    one year rolling count of unique values by group in pandas

  29. 29

    Is there a way to use the unique values of a count of occurences into column headers pandas?

HotTag

Archive