Reputation: 13
Say I have a huge dataset with multiple columns. I would like to create a function that checks the data type of the columns in a dataframe. if the data type is non numeric such as a object/ string then I would like to run a countplot for that column. Once that is done it should move on to the new column and do the same. if the column is of numeric data type then it should plot a histogram for that column.
what would be the approach to solve this?
Upvotes: 1
Views: 183
Reputation: 76
functions
def plot_hist(df, col):
fig = plt.figure()
ax=sns.displot(x=col, data=df)
plt.show()
plt.close(fig)
def plot_count(df, col):
fig = plt.figure()
ax=sns.countplot(x=col, data=df)
plt.show()
plt.close(fig)
def plot_column(df, col):
if (df[col].dtype =='float64') or (df[col].dtype =='int64'):
plot_hist(df, col)
elif (df[col].dtype =='O') :
plot_count(df, col)
else:
print (col+ ' not plotted')
and loop through each column:
for i in df.columns:
plot_column(df, i)
Upvotes: 1