Ashley Tam
Ashley Tam

Reputation: 41

Iterate over certain columns with unique values and generate plots python

New to pandas and much help would be appreciated. I'm currently analyzing some Airbnb data and have over 50 different columns. Some of these columns have tens of thousands of unique values while some have very few unique values (categorical).

How do I loop over the columns that have less than 10 unique values to generate plots for them?

Count of unique values in each column: 
id                                              38185
last_scraped                                        3
name                                            36774
description                                     34061
neighborhood_overview                           18479
picture_url                                     37010
host_since                                       4316
host_location                                    1740
host_about                                      14178
host_response_time                                  4
host_response_rate                                 78
host_acceptance_rate                              101
host_is_superhost                                   2
host_neighbourhood                                486
host_total_listings_count                          92
host_verifications                                525
host_has_profile_pic                                2
host_identity_verified                              2
neighbourhood_cleansed                            222
neighbourhood_group_cleansed                        5
property_type                                      80
room_type                                           4

The above is stored through unique_vals = df.nunique()

Apologies if this is a repeat question, the closest answer I could find was Iterate through columns to generate separate plots in python but it pertained to the entire data set

Thanks!

Upvotes: 0

Views: 439

Answers (1)

user15398259
user15398259

Reputation:

You can filter the columns using df.columns[ unique_vals < 10 ]

You can also pass the df.nunique() call directly if you wish:

unique_columns = df.columns[ df.nunique() < 10 ]

Upvotes: 2

Related Questions