S
S
Sergey Ilyin2021-07-23 11:21:09
Python
Sergey Ilyin, 2021-07-23 11:21:09

How to sum data into a column without grouping?

I have a dataset:

d = {
    'dt': [
        '01.01.2020',
        '01.01.2020',
        '01.01.2020',
        '01.01.2020',
        '02.01.2020',
        '02.01.2020',
        '02.01.2020',
        '02.01.2020',
        '02.01.2020',
        '03.01.2020',
        '03.01.2020',
        '03.01.2020',
        ], 
    'name': [
        'Billy',
        'Dilly',
        'Villy',
        'Villy',
        'Billy',
        'Dilly',
        'Villy',
        'Billy',
        'Dilly',
        'Villy',
        'Billy',
        'Dilly']}

test = pd.DataFrame(data=d)

I would like to get a column 'count'that
contains the number of names on that day.

That is something along the lines of:
d = {
    'dt': [
        '01.01.2020',
        '01.01.2020',
        '01.01.2020',
        '01.01.2020',
        '02.01.2020',
        '02.01.2020',
        '02.01.2020',
        '02.01.2020',
        '02.01.2020',
        '03.01.2020',
        '03.01.2020',
        '03.01.2020',
        ], 
    'name': [
        'Billy',
        'Dilly',
        'Villy',
        'Villy',
        'Billy',
        'Dilly',
        'Villy',
        'Billy',
        'Dilly',
        'Villy',
        'Billy',
        'Dilly'],
    'count': [
        4,
        4,
        4,
        4,
        5,
        5,
        5,
        5,
        5,
        3,
        3,
        3],
    
    }

test = pd.DataFrame(data=d)


but not by hand

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aRegius, 2021-07-23
@sunsexsurf

test['count'] = test.groupby('dt')['dt'].transform('count')

S
Sergey Ilyin, 2021-07-23
@sunsexsurf

the question is described here:
https://stackoverflow.com/questions/57874745/how-t...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question