pie chart drawing for a specific column in pandas python

Advertisements

I have a dataframe df, which has many columns. In df["house_electricity"], there are values like 1,0 or blank/NA. I want to plot the column in terms of a pie chart, where percentage of only 1 and 0 will be shown. Similarly I want to plot another pie chart where percentage of 1,0 and blank/N.A all will be there.

customer_id house_electricity house_refrigerator
cid01 0 0
cid02 1 na
cid03 1
cid04 1
cid05 na 0
#I wrote the following but it didnt give my my expected result
import pandas as pd
import matplotlib.pyplot as plt

df=pd.read_csv("my_file.csv")

df_col=df.columns



df["house_electricity"].plot(kind="pie")
#I wrote the following but it didnt give my my expected result
import pandas as pd
import matplotlib.pyplot as plt

df=pd.read_csv("my_file.csv")

df_col=df.columns



df["house_electricity"].plot(kind="pie")

>Solution :

For a dataframe

df = pd.DataFrame({'a':[1,0,np.nan,1,1,1,'',0,0,np.nan]})
df

a
0   1
1   0
2   NaN
3   1
4   1
5   1
6   
7   0
8   0
9   NaN

The code below will give

df["a"].value_counts(dropna=False).plot(kind="pie")

Leave a ReplyCancel reply