【问题标题】:How to group data with similar dates in pandas如何在熊猫中对具有相似日期的数据进行分组
【发布时间】:2021-09-17 06:44:49
【问题描述】:

我有两个 csv 文件。这两个文件都包含单日的日期、股票、开盘价、最高价、最低价、收盘价列。我从这两个文件中制作了一个数据框。因此,在这个单一的数据帧 1 中,股票 1 的数据从开盘日到收盘时打印,然后股票 2 的数据从开盘日到收盘时打印。数据间隔为 15 分钟,一天从 2019-01-01 开始09:15:00 到 2019-01-01 15:15:00 结束。

我想要的是创建一个数据框,其中打印 2019-01-01 09:15:00 的 stock1 数据,然后同时打印 stock2 的数据,以此类推 2019-01-01 09:30 :00, 2019-01-01 09:45:00....

检查图像:

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    新答案:

    阅读您的回复后,我认为解决您的问题的最佳做法是使用 Pandas MultiIndex 将您的数据移动到 2 索引 DataFrame 格式

    arrays = [
        np.array(["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"]),
        np.array(["one", "two", "one", "two", "one", "two", "one", "two"]),
    ]
    
    df = pd.DataFrame(np.random.randn(8, 4), index=arrays)
    
    df
    Out[16]: 
                    0         1         2         3
    bar one -0.424972  0.567020  0.276232 -1.087401
        two -0.673690  0.113648 -1.478427  0.524988
    baz one  0.404705  0.577046 -1.715002 -1.039268
        two -0.370647 -1.157892 -1.344312  0.844885
    foo one  1.075770 -0.109050  1.643563 -1.469388
        two  0.357021 -0.674600 -1.776904 -0.968914
    qux one -1.294524  0.413738  0.276662 -0.472035
        two -0.013960 -0.362543 -0.006154 -0.923061
    

    旧答案


    您可以使用 pandas concat 方法。如果它们的索引格式匹配,Pandas API 会处理其余的。

    import pandas as pd
    import datetime
    
    idx = pd.date_range("2018-01-01", periods=5, freq="H")
    ts = pd.DataFrame(range(len(idx)), index=idx)
    
    |                     |   0 |
    |:--------------------|----:|
    | 2018-01-01 00:00:00 |   0 |
    | 2018-01-01 01:00:00 |   1 |
    | 2018-01-01 02:00:00 |   2 |
    | 2018-01-01 03:00:00 |   3 |
    | 2018-01-01 04:00:00 |   4 |
    
    idy = pd.date_range("2018-01-02", periods=10, freq="H")
    tsy = pd.DataFrame(range(len(idy)), index=idy)
    
    |                     |   0 |
    |:--------------------|----:|
    | 2018-01-02 00:00:00 |   0 |
    | 2018-01-02 01:00:00 |   1 |
    | 2018-01-02 02:00:00 |   2 |
    | 2018-01-02 03:00:00 |   3 |
    | 2018-01-02 04:00:00 |   4 |
    | 2018-01-02 05:00:00 |   5 |
    | 2018-01-02 06:00:00 |   6 |
    | 2018-01-02 07:00:00 |   7 |
    | 2018-01-02 08:00:00 |   8 |
    | 2018-01-02 09:00:00 |   9 |
    

    结果:

    pd.concat([ts, tsy])
    
    |                     |   0 |
    |:--------------------|----:|
    | 2018-01-01 00:00:00 |   0 |
    | 2018-01-01 01:00:00 |   1 |
    | 2018-01-01 02:00:00 |   2 |
    | 2018-01-01 03:00:00 |   3 |
    | 2018-01-01 04:00:00 |   4 |
    | 2018-01-02 00:00:00 |   0 |
    | 2018-01-02 01:00:00 |   1 |
    | 2018-01-02 02:00:00 |   2 |
    | 2018-01-02 03:00:00 |   3 |
    | 2018-01-02 04:00:00 |   4 |
    | 2018-01-02 05:00:00 |   5 |
    | 2018-01-02 06:00:00 |   6 |
    | 2018-01-02 07:00:00 |   7 |
    | 2018-01-02 08:00:00 |   8 |
    | 2018-01-02 09:00:00 |   9 |
    

    【讨论】:

    • 我已经使用 concat 创建了一个数据框。我想要的是根据日期订购它们。例如,Tatasteel 9:15 的第一个数据,Tatamotors 9:15 的数据等等。
    • 哦,那么我建议您使用 pandas MultiIndex 将数据移动到 2-index DataFrame
    猜你喜欢
    • 2020-07-10
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 2022-01-25
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多