按另一列的标准拆分的数据帧列的条形图

2024-10-02 10:23:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建由另一列的标准分割的指定数据帧列的条形图(此处<;5). 它的工作原理如下,但肯定有一种更像数据帧的方式?类似于df.makeCoolBarPlots()的内容

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

df = pd.DataFrame({'score':[1,6,2,3,1,9,5], 'age':[10,16,33,12,8,24,18], 'IQ':[89,120,88,94,103,110,102]})

df_pass = df[df['score'] >= 5]
df_fail = df[df['score'] < 5]


fieldsOfInterest = ['age', 'IQ']
ind = np.arange(2)

for fieldOfInterest in fieldsOfInterest:
    plt.figure()
    plt.bar(ind, [df_pass[fieldOfInterest].mean(), df_fail[fieldOfInterest].mean()], yerr=[df_pass[fieldOfInterest].std(), df_fail[fieldOfInterest].std()])
    stat, p = stats.ttest_ind(df_pass[fieldOfInterest], df_fail[fieldOfInterest])
    plt.title("p={:0.3f}".format(p))
    plt.xticks(ind, ('pass', 'fail'))
    plt.ylabel(fieldOfInterest)

plt.show()

Tags: 数据importdfageasstatsnpplt
1条回答
网友
1楼 · 发布于 2024-10-02 10:23:04

您可以在^{}旁边使用pandas内置的^{}函数:

# First make your conditions using np.select
df["group"] = np.select([df["score"].ge(5), df["score"].lt(5)], ["pass", "fail"])

# Create a groupby
gb = df.groupby('group')

for col in ["age", "IQ"]:
    # Get p value, mean, and std for each column
    _, p = stats.ttest_ind(*[g[col] for n, g in gb])
    means = gb[col].mean()
    errors = gb[col].std()

    # Plot using pandas.plot
    fig, ax = plt.subplots()
    means.plot.bar(yerr=errors, ax=ax)
    ax.set(ylabel=col, title="p={:.3f}".format(p))

结果:

enter image description here

相关问题 更多 >

    热门问题