Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

pandas - create a function to plot countplot or plot a histogram based on the data type of the column in a dataframe

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?

question from:https://stackoverflow.com/questions/65895806/create-a-function-to-plot-countplot-or-plot-a-histogram-based-on-the-data-type-o

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...