【问题标题】:how to compare columns in data frame如何比较数据框中的列
【发布时间】:2022-01-24 15:11:41
【问题描述】:

我试图直观地比较数据框中的两列,它要么用“频率”而不是其中一列制作一个奇怪的表

我尝试了以下选项:

ct1=pd.crosstab(df['releaseyear'],df['score'],normalize=True)
ct1.plot()

df.plot( x='releaseyear', y='score', kind='hist')

还有一个散点图,它使 x 和 y 正确,但我不知道如何对其进行标准化,所以它只会显示每年的平均值,而不是所有数据

plt.scatter(df['releaseyear'], df['score'])
plt.show()

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    没有适当的数据可用于重现数据框或有关数据框外观的线索。

    这个答案是根据我的理解,如果数据是这样的

       year score
       2001 20
       2001 18
       2002 12
       2002 16
    

    然后先使用groupby,按年份分组数据,应用所需的聚合函数。

    df=df.groupby('year').mean().reset_index()
    

    输出

       year  score
    0  2001   19.0
    1  2002   14.0
    

    然后您可以相应地绘制数据。

    【讨论】:

    • 谢谢,这有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 2022-11-13
    • 1970-01-01
    • 2022-11-16
    相关资源
    最近更新 更多