Skip to main content

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 the turnout_tier on.

Returns:

  • pandas.DataFrame: The DataFrame with the new turnout_tier column 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

turnout_tier_categorization-0.2.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

turnout_tier_categorization-0.2.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file turnout_tier_categorization-0.2.0.tar.gz.

File metadata

File hashes

Hashes for turnout_tier_categorization-0.2.0.tar.gz
Algorithm Hash digest
SHA256 affac3b25b6da1f56eeeb515214c202fbf4cd41b1d7d0bd22de525e99bdec119
MD5 810ffaa8eee51b67c659a2f726b80e91
BLAKE2b-256 efcc9825a90376d7b332b9602a92d6cd13a665836545f60c6866f778f34bb1ac

See more details on using hashes here.

File details

Details for the file turnout_tier_categorization-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for turnout_tier_categorization-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 463eba54b0983d1edf97b671660ee015ab8adf7e02487a28596c1cb017504e43
MD5 c753c0f1f123087973c0eeb12f749fae
BLAKE2b-256 c7205c1e73f2de6daee1275b71bdceb7841e6f29c6f41beb9a22e3681707aec8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page