How to create bar graph using Dataframe.Plot?

Asked

Viewed 904 times

1

I’m trying to chart a table comparing the rate of uneducated men to the rate of uneducated women. I have a csv file with this data and I’m trying to put men and women on the "x" line and the number of illiterate people on the "y" line, so far my code is like this:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("https://drive.google.com/uc?export=download&id=1k_kvlZ6zpnMeHUoA7GCEoMkKc7ybQLCX", sep=';' , encoding='latin-1')
df["qnthomens"] = df["HomensNaoAlfabetRural"] + df["HomensNaoAlfabetUrb"]
df["qntmulheres"] = df["MulheresNaoAlfabetRural"] + df["MulheresNaoAlfabetUrb"]
quantidade = df["qntmulheres"] and df["qnthomens"]
df["quantidade"] = quantidade
sexo = ['Homens', 'Mulheres']
df["sexo"] = sexo


plt.title('Homens x Mulheres')
df.plot(kind='bar',x='sexo',y='quantidade')
plt.show()

I’m trying to create a "quantity" column and put two different values within it, the sum of illiterate men from urban and rural areas, and also the sum of illiterate women from the same areas

Already in the "sex" column I created I inserted "men" and "women" to stay in line "x" of the chart, but report the error

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

I’ve tried so many ways and I have no idea what to do, I’m new at this and I can’t find anything on the Internet...

  • I don’t think you need to use the dataframe Plot in this case. Note that you don’t need the whole column, just the sum of each one. By the way: the error is being triggered by the line quantidade = df["qntmulheres"] and df["qnthomens"]. The logical expression should be made elementwise, but my bet is that’s not what you want to do.

1 answer

1


If I understand what you want to do, you don’t need the DataFrame.plot. It’s simpler than that:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("tabela_trabalho2.csv", sep=';' , encoding='latin-1')
df["qnthomens"] = df["HomensNaoAlfabetRural"] + df["HomensNaoAlfabetUrb"]
df["qntmulheres"] = df["MulheresNaoAlfabetRural"] + df["MulheresNaoAlfabetUrb"]

x=[1,2]
plt.title('Homens x Mulheres')
plt.bar(x,height=[df["qnthomens"].sum(),df["qntmulheres"].sum()] )
plt.xticks(x, ('Homens','Mulheres'))
plt.show()

inserir a descrição da imagem aqui

The implementation of the bar graph with matplotlib directly has the benefit of being simpler. According to the documentation, the plt.bar only needs a sequence of scalars (which you can rename later) and a list of bar heights.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.