1

My goal is to create a new column that includes the total repetitions per date.

Before

     date
0  6/1/18
1  6/1/18
2  6/4/18
3  6/5/18
4  6/6/18
6  6/6/18
7  6/6/18

After

     date  count
0  6/1/18    2
1  6/1/18    2
2  6/4/18    1
3  6/5/18    1
4  6/6/18    3
6  6/6/18    3
7  6/6/18    3

I tried using some similar solutions on this site to fit my criteria to no luck.

# Possible format I can use, but not what I am looking for.
df.loc[df['date'] == 1, 'b'].sum()
# I changed it to this to this to no solution.
df['Count'] = df.loc[df['date'] == df['date']].sum()

Perhaps I am on the wrong path, but any insight would be appreciated.

1
  • to no solution. What does that mean? Please provide a minimal reproducible example, and see How to Ask, help center. create a new column that includes the total repetitions per date Why keep that same format, then, instead of just the unique dates? Commented May 4, 2020 at 0:56

2 Answers 2

3

Let us do

df['count']=df.date.groupby(df.date).transform('count')
df
     date  count
0  6/1/18      2
1  6/1/18      2
2  6/4/18      1
3  6/5/18      1
4  6/6/18      3
6  6/6/18      3
7  6/6/18      3
Sign up to request clarification or add additional context in comments.

Comments

0

If you're trying to get rid of the duplicates, but keep the count...

import pandas as pd

data = ['6/1/18', '6/1/18', '6/4/18', '6/5/18', '6/6/18', '6/6/18','6/6/18']

df = pd.DataFrame({'Date': data})
df = df.pivot_table(columns=['Date'], aggfunc='size').reset_index()
df.rename(columns={0: 'Count'})

     Date  Count
0  6/1/18      2
1  6/4/18      1
2  6/5/18      1
3  6/6/18      3

2 Comments

ur answer doesnt seem to match what the OP wants
the OPs "after" sample didn't make sense to me, this does.

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.