Reputation: 117
I have a df that has 5 categorical values and I am doing the following the count the number of values and then plot them:
data['yr'].value_counts()
sns.countplot(x = 'yr', data = data)
data['season'].value_counts()
sns.countplot(x = 'season', data = data)
data['holiday'].value_counts()
sns.countplot(x = 'holiday', data = data)
data['workingday'].value_counts()
sns.countplot(x = 'workingday', data = data)
data['weathersit'].value_counts()
sns.countplot(x = 'weathersit', data = data)
In this case there are only 5 columns, so this technique was not cumbersome. But if there are many such features, writing like this will be highly cumbersome. So, I would like to know how can I modify the above code to make it more efficient?
Upvotes: 0
Views: 43
Reputation: 100
Considering you loaded your dataframe using pandas you could use this piece of code which will also get you a list of all the column names in your
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = pd.read_csv('cars.csv' , ';')
# Gets you the list of all columns in your dataframe.
columns = list(data.columns.values.tolist())
print(columns)
for column in columns:
data[column].value_counts()
sns.countplot(x = column, data = data)
plt.show()
plt.close()
Upvotes: 1
Reputation: 627
you can try like this:
columns = ["yr","season","holiday","workingday","weathersit"]
for column in columns:
data[column].value_counts()
sns.countplot(x = column, data = data)
Upvotes: 1