A function to add a turnout tier categorization column to a pandas DataFrame
Project description
Turnout Tier Categorization
This Python script contains a function add_turnout_tier that adds a new categorical column called turnout_tier to a pandas DataFrame. The categorization is based on the values in a specified numeric column.
Function Description
add_turnout_tier
Adds a new categorical column turnout_tier to the DataFrame based on the specified numeric column.
Parameters:
df(pandas.DataFrame): The DataFrame containing the numeric column.numeric_column(str): The name of the numeric column to base theturnout_tieron.
Returns:
- pandas.DataFrame: The DataFrame with the new
turnout_tiercolumn added.
Turnout Tier Criteria:
- > 80: "Very likely to vote regardless of campaign"
- 70-79: "Turnout Tier 1"
- 50-69: "Turnout Tier 2"
- 30-59: "Turnout Tier 3"
- < 30: "Very unlikely to vote regardless of campaign"
Usage
Example
import pandas as pd
# Define the function
def add_turnout_tier(df, numeric_column):
def categorize_turnout(value):
if value > 80:
return "Very likely to vote regardless of campaign"
elif 70 <= value <= 79:
return "Turnout Tier 1"
elif 50 <= value <= 69:
return "Turnout Tier 2"
elif 30 <= value <= 59:
return "Turnout Tier 3"
else:
return "Very unlikely to vote regardless of campaign"
df['turnout_tier'] = df[numeric_column].apply(categorize_turnout)
return df
# Example usage
data = {
'voter_score': [85, 75, 60, 45, 25]
}
df = pd.DataFrame(data)
df = add_turnout_tier(df, 'voter_score')
print(df)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file turnout_tier_categorization-0.1.0.tar.gz.
File metadata
- Download URL: turnout_tier_categorization-0.1.0.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67f15adb16b8bec7033932a667754d7f3aef602ebd68d217a12ed56fc09dca74
|
|
| MD5 |
b21dbb6a4684a7ad180d3915bd75c3c3
|
|
| BLAKE2b-256 |
ae9fb3a1a44b5d12ed0d619e2bbaf69eadca81bcda679207e507a67b6bca544e
|
File details
Details for the file turnout_tier_categorization-0.1.0-py3-none-any.whl.
File metadata
- Download URL: turnout_tier_categorization-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ece0500461ba3e9f31411174142e5aa9a600d1dd2fb98ebfa917cd12aff6013
|
|
| MD5 |
e2c6ec2917f36454274f2fea57d7010f
|
|
| BLAKE2b-256 |
079b17b9c26d29f89b0251c1308ae3972f064cd8163a37583b15d6771a426d3e
|