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.1.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.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for turnout_tier_categorization-0.1.0.tar.gz
Algorithm Hash digest
SHA256 67f15adb16b8bec7033932a667754d7f3aef602ebd68d217a12ed56fc09dca74
MD5 b21dbb6a4684a7ad180d3915bd75c3c3
BLAKE2b-256 ae9fb3a1a44b5d12ed0d619e2bbaf69eadca81bcda679207e507a67b6bca544e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for turnout_tier_categorization-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9ece0500461ba3e9f31411174142e5aa9a600d1dd2fb98ebfa917cd12aff6013
MD5 e2c6ec2917f36454274f2fea57d7010f
BLAKE2b-256 079b17b9c26d29f89b0251c1308ae3972f064cd8163a37583b15d6771a426d3e

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