Master Onboarding Insights: Quick Pandas Go-To Functions
The handful of Pandas functions that do most of the work in onboarding analysis — aggregating by device, standardising statuses, and calculating per-country conversion rates.
Data analysis is crucial for understanding user behavior and driving successful app development. As we navigate the complexities of user interactions, utilizing Python’s powerful data manipulation libraries, such as Pandas, allows us to gain valuable insights from our datasets. In this article, we explore essential Pandas functions and techniques for onboarding analysis, focusing on the aggregation of user data and the identification of key trends. Through practical examples, we will demonstrate how to clean, group, and analyze data to inform strategic decisions and optimize user experience.
Data aggregation and cleanup in Pandas
This approach combines unique values across multiple columns while grouping data by deviceID, ensuring no duplicates or NaN values get in the way. By applying a custom join_unique_values() function, we can simplify data cleanup and aggregation in one step.
def join_unique_values(series):
return ', '.join(series.dropna().astype(str).unique())
combined_interests = df.groupby('deviceID').agg({
'country_code': join_unique_values,
'triggerfrom': join_unique_values,
'status': join_unique_values,
'transaction': join_unique_values
}).reset_index()
Filtering and cleaning with replace and contains
This code replaces any instance of 'done, error in purchase' in the status column with 'done' to standardize the status values. It then filters the data to include only rows where status contains the word “done”, regardless of case, ensuring a clean dataset focused on completed transactions.
data['status'] = data['status'].replace('done, error in purchase', 'done')
filtered_data = data[data['status'].str.contains('done', case=False, na=False)]
Country-based aggregation and conversion rate calculation
This approach groups data by country_code to analyze transaction statuses, providing insights into success and error rates. Using agg, it calculates the total count of records per country, counts rows with “done” and “error in purchase” statuses, and calculates conversion rates for each status:
- total_count — total records for each
country_code. - done_count — rows where status is “done”.
- error_count — rows where status is “error in purchase”.
The counting of done and error statuses is done using lambda functions, which check for specific conditions and sum up the True values. Finally, conversion rates are calculated as percentages, showing the success (done) and error (error in purchase) rates per country.
summary = df_filtered.groupby('country_code').agg(
total_count=('status', 'count'),
done_count=('status', lambda x: (x == 'done').sum()),
error_count=('status', lambda x: (x == 'error in purchase').sum())
).reset_index()
summary['cvr_done'] = summary['done_count'] / summary['total_count'] * 100
summary['cvr_error'] = summary['error_count'] / summary['total_count'] * 100
Mapping country codes using a dictionary
This code snippet defines a mapping between country names and their corresponding country codes using a dictionary called country_code_map. It then creates a reverse_country_code_map that allows for easy lookup of country names based on country codes.
- Country code mapping: the
country_code_mapdictionary associates each country with its ISO code (e.g. ‘United States’ → ‘US’). - Reverse dictionary creation: the
reverse_country_code_mapis generated by swapping keys and values from the original dictionary, enabling lookups from code to country name. - Mapping in DataFrame: the
mapfunction is applied to thecountryCodecolumn infinal_df, replacing each country code with its corresponding country name from thereverse_country_code_map. This ensures that the DataFrame contains user-friendly country names rather than codes, enhancing readability and analysis.
country_code_map = {
'Afghanistan': 'AF', 'Albania': 'AL', 'Algeria': 'DZ', 'Andorra': 'AD', 'Angola': 'AO',
'Antigua and Barbuda': 'AG', 'Argentina': 'AR', 'Armenia': 'AM', 'Australia': 'AU',
'Austria': 'AT', 'Azerbaijan': 'AZ', 'Bahamas': 'BS', 'Bahrain': 'BH', 'Bangladesh': 'BD',
'Barbados': 'BB', 'Belarus': 'BY', 'Belgium': 'BE', 'Belize': 'BZ', 'Benin': 'BJ',
'Bhutan': 'BT', 'Bolivia': 'BO', 'Bosnia and Herzegovina': 'BA', 'Botswana': 'BW',
'Brazil': 'BR', 'Brunei': 'BN', 'Bulgaria': 'BG', 'Burkina Faso': 'BF', 'Burundi': 'BI',
'Cabo Verde': 'CV', 'Cambodia': 'KH', 'Cameroon': 'CM', 'Canada': 'CA'
}
reverse_country_code_map = {v: k for k, v in country_code_map.items()}
final_df['countryCode'] = final_df['countryCode'].map(reverse_country_code_map)
Random selection function for data handling
The following code defines a function called pick_random_category that randomly selects one category from a list of categories in a given row.
- NaN check: the function first checks if the row value is NaN (not a number). If it is, the function returns NaN to maintain data integrity.
- Splitting categories: if the row contains a string of categories, it splits this string into a list using
', 'as the delimiter. - Random selection: the function then uses
random.choice()to randomly pick one category from the list. This approach is useful when dealing with data entries that may belong to multiple categories, ensuring that a single representative category is selected for further analysis or processing.
def pick_random_category(row):
if pd.isna(row):
return row
categories = row.split(', ')
return random.choice(categories)
Conclusion
Leveraging Pandas for onboarding analysis equips developers and analysts with the tools needed to dissect and interpret user data effectively. By applying functions like aggregation and random selection, we can streamline our data processes, extract meaningful insights, and ultimately enhance user engagement. As we continue to explore the nuances of user behavior through data, we empower ourselves to create better, more user-centric apps. With these techniques, you’re well on your way to making data-informed decisions that drive app success.
First published on LinkedIn ↗