I have a data frame and I need to group by 2 columns and create a new column based on condition. My data looks like this:
ID | week | day_num |
---|---|---|
1 | 1 | 2 |
1 | 1 | 3 |
1 | 2 | 4 |
1 | 2 | 1 |
2 | 1 | 1 |
2 | 2 | 2 |
3 | 1 | 4 |
I need to group by the columns ID & week so there's a row for each ID for each week. The groupby is based on condition- if for a certain week an ID has the value 1 in column day_num, the value will be 1 under groupby, otherwise 0. For example, ID 1 has 2 & 3 under both rows so it equals 0 under groupby, for week 2 ID 1 it has a row with value 1, so 1.
The output I need looks like this:
ID | week | day1 |
---|---|---|
1 | 1 | 0 |
1 | 2 | 1 |
2 | 1 | 1 |
2 | 2 | 0 |
3 | 1 | 0 |
I searched and found this code, but it uses count, where I just need to write the value 1 or 0.
df1=df1.groupby('ID','week')['day_num'].apply(lambda x: (x=='1').count())
Is there a way to do this?
Thanks!