【问题标题】:Plotting and GroupBy methods绘图和 GroupBy 方法
【发布时间】:2013-07-11 07:20:45
【问题描述】:

我刚刚学习利用 Pandas 中的 DataFrames,我想使用 GroupBy 方法生成以下图表:

我有两个数据框,一个用于 x 轴信息,一个用于 y。在每个数据框中,有三个版本的数据,比如“A”、“B”、“C”。我需要为每一个(即三行)绘制一个 y 与 x 的图。

示例代码:

df_x

<class 'pandas.core.frame.DataFrame'>
Int64Index: 100 entries, 0 to 99
Data columns (total 3 columns):
A     100  non-null values
B      100  non-null values
C    100  non-null values
dtypes: float64(2), object(1)

df_y

<class 'pandas.core.frame.DataFrame'>
Int64Index: 100 entries, 0 to 99
Data columns (total 3 columns):
A     100  non-null values
B      100  non-null values
C    100  non-null values
dtypes: float64(2), object(1)

有没有一种快速的方法来生成所需的绘图,避免循环并使用 Pandas 方法?我正在考虑合并两个框架并使用 GroupBy 方法,但我不知道如何去做。

谢谢!

【问题讨论】:

    标签: python matplotlib pandas


    【解决方案1】:

    我认为你可以直接使用 pyplot 来绘制这个图:

    In [11]: plot(df_x, df_y)  # matplotlib.pyplot.plot
    Out[11]:
    [<matplotlib.lines.Line2D at 0x109c02910>,
     <matplotlib.lines.Line2D at 0x109c02b90>,
     <matplotlib.lines.Line2D at 0x109c02ed0>]
    

    看来你需要在之后设置图例:

    pylab.legend(df_x.columns)
    

    如果你真的想将你的数据重塑成一个表格来使用.plot,也许你可以使用:

    In [21]: df_x = pd.DataFrame([[1,2,1],[2,3,4]], columns=list('ABC'))
    
    In [22]: df_y = pd.DataFrame([[2,6,1],[4,9,4]], columns=list('ABC'))
    
    In [23]: pd.DataFrame({'x': df_x.stack(), 'y': df_y.stack()}).reset_index(level=1).pivot('x', 'level_1', 'y')
    Out[23]:
    level_1   A   B   C
    x
    1         2 NaN   1
    2         4   6 NaN
    3       NaN   9 NaN
    4       NaN NaN   4
    

    这将大大降低效率(占用比它需要的更多的空间),因为它包含大量丢失的数据。

    【讨论】:

    • 行得通!我想我对 GroupBy 方法太兴奋了,想在任何地方使用它们。但是,如何为每一列传递绘图样式?
    猜你喜欢
    • 2018-09-26
    • 2022-01-22
    • 2017-12-28
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2019-02-13
    • 2017-06-13
    相关资源
    最近更新 更多