0

I have a pandas dataframe that has an int64 column which has values either 0 or 1. And another object column that has different strings.

dataframe screenshot:

enter image description here

I want to plot a graph (preferably bar or pie) that will show how many values are equal to 1, and how many are equal to 0 in that int64 column. Also I would like to legend them as 1 - Democrats, 0 - Republicans.

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
  • Welcome to StackOverflow. Please do not use images of dataframes; instead provide code to replicate them. see [this](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for more hints. Also please re-review [how to ask](https://stackoverflow.com/help/how-to-ask), and show us what you have tried so far. – piterbarg Mar 28 '21 at 13:23

1 Answers1

0

To reproduce your df let me do

import numpy as np
import pandas as pd

np.random.seed(42)
df = pd.DataFrame({
    'Party': np.random.choice([0, 1], 1000)
})

now, map the [0,1] to wanted categories

df['Party_Name'] = df.Party.map({0: 'Republicans', 1: 'Democrats'})

count the values and plot the pie

df.Party_Name.value_counts().plot.pie(
    y='Party_Name',
    autopct='%1.0f%%'
);

enter image description here

Max Pierini
  • 2,027
  • 11
  • 17