I have a dataframe with the following columns: ID, event_name, event_date
Goal: For every unique ID, if they have an event_name == 'attended book event'
then I want to create a new column attended_book_event
and have the value = 1
. If they do not have and event_name==' attended book event'
then the value in the new column is 0
.
Sample:
ID| event_name | event_date
1| joined_club| 12-12-03
1| attended_book_event| 12-27-03
1| elite_member| 03-01-05
2| joined_club| 12-12-03
2| elite_member| 03-01-05
I tried to groupby the id and then create a new column with the condition but the results were not what I was looking for.
df_dose['had_dose_increase'] = [1 if df_dose['event_name'] ==
'dose_increased' else 0]
I want a new column
ID| event_name | event_date| attended_book_event
1| joined_club| 12-12-03| 1
1| attended_book_event| 12-27-03|1
1| elite_member| 03-01-05|1
2| joined_club| 12-12-03|0
2| elite_member| 03-01-05|0
Using pd.Series.groupby
with transform
:
df['attended_book_event'] = df.groupby('ID')['event_name'].transform(lambda x: 'attended_book_event' in set(x)).astype(int)
Output:
ID event_name event_date attended_book_event
0 1 joined_club 12-12-03 1
1 1 attended_book_event 12-27-03 1
2 1 elite_member 03-01-05 1
3 2 joined_club 12-12-03 0
4 2 elite_member 03-01-05 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.