Get startedGet started for free

Adding the donor segment

Besides age, you also want to add the segment of a donor to the basetable. A selected group of donors that has made many donations in the past is assigned a segment: bronze, silver or gold. Given is an early stage basetable and a pandas dataframe segments that contains the segments for a selected group of the donors in the basetable. In this exercise you will add the segment to the basetable.

You can left join two pandas dataframes using the following code:

merged_df = pd.merge(df1, df2, on=["merge_id"],how="left")

This exercise is part of the course

Intermediate Predictive Analytics in Python

View Course

Exercise instructions

  • The donor id column is "donor_id" both in segments and basetable. Add the segment to the basetable.
  • Count the number of donors in each segment.
  • Count the number of donors with no segment assigned.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Add the donor segment to the basetable
basetable = pd.merge(____, ____, on =["____"], how="____")

# Count the number of donors in each segment
basetable.groupby("____").____()

# Count the number of donors with no segment assigned
print(basetable["segment"].____().____())
Edit and Run Code