【问题标题】:Efficiently plotting multiple columns in pandas在熊猫中有效地绘制多列
【发布时间】:2014-10-22 22:15:24
【问题描述】:

我想知道如何在 pandas 数据框中有效地绘制多列组。

我有以下数据框

        | a | b | c |...|trial1.1|trial1.2|...|trial1.12|trial2.1|...|trial2.12|trial3.1|...|trial3.12|
GlobalID|
sd12f   |...|...|...|...| 210.1  | 213.1  |...| 170.1   | 176.2  |...| 160.31  | 162.4  |...| 186.1   | 
...

我想遍历这些行并为每一行绘制三个波形:trial1.[1-12]trial2.[1-12]trial3.[1-12]。最有效的方法是什么?现在我有:

t1 = df.ix[0][df.columns[[colname.startswith('trial1') for colname in df]]]
t2 = df.ix[0][df.columns[[colname.startswith('trial2') for colname in df]]]
t3 = df.ix[0][df.columns[[colname.startswith('trial3') for colname in df]]]
t1.astype(float).plot()
t2.astype(float).plot()
t3.astype(float).plot()

我需要.astype(float),因为这些值最初是字符串。我错过了一些更有效的方法吗?我是 python 和 pandas 的新手。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    先横切数据帧,然后通过试验分割数据帧,然后绘图。

    # Transverse
    data = pd.read_csv("data.txt").T
    
    # Insert your code to remove irrelevant rows, like a, b, c in your example
    #
    
    # Group by the trial number (the first six characters) and plot
    data.groupby(lambda x: x[:6], axis=0).plot()
    

    【讨论】:

    • 现在我通过t1 = df.ix[0][df.columns[[colname.startswith('trial1') for colname in df]]] 删除不相关的行,这是最好的方法吗?
    • 这绝对有效,或者您可以执行 df.index.map(lambda x: x.startswith('trial'))。这里 df 是横向数据框。
    猜你喜欢
    • 2018-05-26
    • 2018-09-26
    • 2020-03-18
    • 2015-09-16
    • 2020-08-27
    • 2020-10-10
    • 1970-01-01
    • 2014-10-24
    相关资源
    最近更新 更多