Home > Mobile >  Binning Percent Ranges with Labels Python Pandas
Binning Percent Ranges with Labels Python Pandas

Time:02-05

I have a column in my dataframe called "Trump2020." This column houses percentages of the votes he received. Example values in this column: 34.66%, 51.00%, 75.00%, etc. I want to bin this column with 3 labels: lose which would be anything less than 50%, so 0% to 49%, win would be 50% to 64% and win by landslide which would be 65% to 1.

How can I make the three categories to the percentage groups?

CodePudding user response:

You can use pandas.cut, as long as your values are numerical (ordered).

    df["Trump2020_bin"] = pd.cut(
        df["Trump2020"],
        [0, 50, 65, 100],
        labels=["lose", "win", "landslide"],
    )
  •  Tags:  
  • Related