0

I have two bars in that I plotted from a csv column. The column name is 'sex' and the values are 0 for male and 1 for female. this is my code:

df = pd.read_csv("file.csv")
df['sex'].value_counts().plot(kind='bar')
plt.show()

I want instead of having 0 and 1 for each bar, I would like to change it the 'Male' and 'Female' respectively.
The output I get is: my plot

Assi
  • 13
  • 3

3 Answers3

3

You can use the code:

df['sex'] = np.where(df['sex'] == 0, 'male', 'female')
Arun
  • 1,874
  • 2
  • 31
  • 59
1

before plotting, you can change the value in the dataframe like this:

df.sex = df.sex.apply(lambda x: 'Male' if x == 0 else 'Female')

Then the plot will apply those labels.

Haleemur Ali
  • 24,060
  • 3
  • 49
  • 77
1

You can use this to manually add costumized labels:

p = df['sex'].value_counts().plot(kind='bar')
p.set_xticklabels(['Male','Female'])
plt.show()
Ale
  • 796
  • 7
  • 25