0
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

Female = df[(df["Gender"] == "Female")]
Male = df[(df["Gender"] == "Male")]

AgeClass = ['100+','95-99','90-94','85-89','80-84','75-79','70-74','65-69','60-64','55-59','50-54','45-49','40-44','35-39','30-34','25-29','20-24','15-19','10-14','5-9','0-4']

bar_plot = sns.barplot(x= Male, y=df['Age'], order=AgeClass)

bar_plot = sns.barplot(x= Female, y=df['Age'], order=AgeClass)

bar_plot.set(xlabel="Population (hundreds of millions)", ylabel="Age-Group", title = "Population Pyramid")

I'm trying to build a population pyramid from a pandas df using seaborn.

I got value error. Help me to fix it

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
asli
  • 25
  • 6

2 Answers2

1

I do not have access to your data-set, so I am not sure. From what I see, a problem may be that the Series you are passing to y has different size than the Series you are passing to x, because x is the result of a mask, and y is a Series from the whole original DataFrame.

You should do something like this:

mask_female = df["Gender"] == "Female"
female = df[mask_female]["Gender"]
age_female = df[mask_female]["Age"]

mask_male = df["Gender"] == "Male"
male = df[mask_male]["Gender"]
age_male = df[mask_male]["Age"]

Then, to the sns.barplot calls, you should pass male and age_male, female and age_female to x and y arguments.

Also, make sure that all values in AgeClass are actually present in the "Age" column.

A minor suggestion: when creating masks like these, I would use the very nice and readable eval method:

mask_male = df.eval("Gender == 'Male'")
Enrico Gandini
  • 855
  • 5
  • 29
0

I believe your problem is coming from order=AgeClass SeaBorn is expecting an array of values that exist in the table not strings of ranges.

kpie
  • 9,588
  • 5
  • 28
  • 50