ZelelB
ZelelB

Reputation: 2010

How to plot the number of unique values in each column in pandas dataframe as bar plot?

I want to plot the count of unique values per column for specific columns of my dataframe.

So if my dataframe has four columns 'col_a', 'col_b' , 'col_c' and 'col_d', and two ('col_a', 'col_b') of them are categorical features, I want to have a bar plot having 'col_a' and 'col_b' in the x-axis, and the count of unique values in 'col_a' and number of unique values in 'col_b' in the y-axis.

PS: I don't want to plot the count of each unique value in a specific column.

Actually, how to bar plot this with python?

properties_no_na.nunique()

Which returns:

neighborhood                 51
block                      6805
lot                        1105
zip_code                     41
residential_units           210
commercial_units             48
total_units                 215
land_sqft_thousands        6192
gross_sqft_thousands       8469
year_built                  170
tax_class_at_sale             4
building_class_at_sale      156
sale_price_millions       14135
sale_date                  4440
sale_month                   12
sale_year                    15
dtype: int64

How would that be possible? If possible with Seaborn?

Upvotes: 1

Views: 5704

Answers (2)

Praveen_streams
Praveen_streams

Reputation: 1

sns.displot(x=df.column_name1,col=df.column_name2,kde=True)

note: sns is the alias of python seaborn library.

x axis always column_name1 and y axis column_name2. And this code will give you number of displots depends on unique values in the column column_name2

Upvotes: 0

Poe Dator
Poe Dator

Reputation: 4912

nunique() returns Pandas.Series. Convert it to Pandas.DataFrame with reset_index() and call seaborn.

nu = properties_no_na.nunique().reset_index()
nu.columns = ['feature','nunique']
ax = sns.barplot(x='feature', y='nunique', data=nu)

Upvotes: 1

Related Questions