0

I have a dataset in Python with columns like Year-month_1 (i.e. 2022-01), Year-month_2 (i.e. 2022-03), a column total with integer values, and a customer_id.

I want to create a new column with the value of the total columns for each customer id for each value of Year-month_1 and Year-month_2.

I tried with:

a.loc[:,'new_column1'] = a.groupby(['Year-month_1','customer_id'], sort=False,as_index=False)['total']
a.loc[:,'new_column2'] = a.groupby(['Year-month_2','customer_id'], sort=False,as_index=False)['total']

but it doesn't work, I have the error: "ValueError: Length of values (1178) does not match the length of index (58779)"

Expect output:

df = pd.DataFrame({'customer_id': ['01', '02','01','01'], 'total': [10,3,20,30],
                   'Year-month_actual': ['2022-05','2022-05','2022-06','2022-07'],
                   'Year-month_1': ['2022-04','2022-04','2022-05','2022-06'],
                   'Year-month_2': ['2022-03','2022-03','2022-04','2022-05'],
                   'Total_month_1': ['Na','Na',10,20]})

0 Answers0