【问题标题】:How to calculate and write the sliding window result to csv?如何计算滑动窗口结果并将其写入csv?
【发布时间】:2021-05-29 18:39:25
【问题描述】:

我有一个这样的 csv 数据文件:

我想得到这样的结果:

我还想将结果保存到 csv 文件。

到目前为止,我尝试使用滑动窗口:

def window(iterable, size=3):
    i = iter(iterable)
    win = []
    for e in range(0, size):
        win.append(next(i))
    yield win
    for e in i:
        win = win[1:] + [e]
        yield win
        
X = [188,122,222,222,222,222,222,222,222,222]
count = 0
for w in window(X):
    print(w)
    count = count + 1
    print(count)
    if count == 10:
        break

【问题讨论】:

    标签: python pandas dataframe data-analysis


    【解决方案1】:

    一种选择是创建自己的滚动聚合函数:

    def rolling_agg_list(src_frame, cols, window):
        frame = src_frame[cols]
        d = {v: list for v in cols}
        values = [frame.iloc[i:i + window].agg(d)
                  for i in range(len(frame) - window + 1)]
        return pd.DataFrame(values, columns=cols)
    
    
    new_df = rolling_agg_list(df, ['X', 'Y', 'Z'], 3)
    

    要写入 CSV,只需在新 DataFrame 上调用 to_csv

    new_df.to_csv('out.csv', index=False)
    

    示例df:

    import numpy as np
    import pandas as pd
    
    np.random.seed(5)
    df = pd.DataFrame({
        'ID': np.arange(1, 11),
        'X': np.random.randint(118, 122, size=10),
        'Y': np.random.random(size=10) * 2 - 1,
        'Z': np.random.random(size=10) * 2 - 1
    })
    

    df:

       ID    X         Y         Z
    0   1  121  0.223488 -0.451827
    1   2  120  0.531816 -0.171530
    2   3  121  0.036836 -0.407840
    3   4  119 -0.406399  0.257576
    4   5  120 -0.624558  0.159676
    5   6  120 -0.838517  0.199858
    6   7  118  0.476881 -0.468362
    7   8  119 -0.117382 -0.430628
    8   9  118 -0.683380 -0.492824
    9  10  118  0.759874 -0.344872
    

    new_df:

                           X                                                                 Y                                                                  Z
    0  [121.0, 120.0, 121.0]   [0.22348772580529142, 0.5318157129606311, 0.036835975745886484]  [-0.4518270760155507, -0.17152996183789737, -0.40784013453270407]
    1  [120.0, 121.0, 119.0]   [0.5318157129606311, 0.036835975745886484, -0.4063989968475561]  [-0.17152996183789737, -0.40784013453270407, 0.25757581775896665]
    2  [121.0, 119.0, 120.0]  [0.036835975745886484, -0.4063989968475561, -0.6245575426774967]    [-0.40784013453270407, 0.25757581775896665, 0.1596756203790901]
    3  [119.0, 120.0, 120.0]   [-0.4063989968475561, -0.6245575426774967, -0.8385174624702503]     [0.25757581775896665, 0.1596756203790901, 0.19985839324997512]
    4  [120.0, 120.0, 118.0]      [-0.6245575426774967, -0.8385174624702503, 0.47688059239794]     [0.1596756203790901, 0.19985839324997512, -0.4683617649289855]
    5  [120.0, 118.0, 119.0]     [-0.8385174624702503, 0.47688059239794, -0.11738155420809382]    [0.19985839324997512, -0.4683617649289855, -0.4306282387172724]
    6  [118.0, 119.0, 118.0]     [0.47688059239794, -0.11738155420809382, -0.6833802645746976]     [-0.4683617649289855, -0.4306282387172724, -0.492823588452425]
    7  [119.0, 118.0, 118.0]   [-0.11738155420809382, -0.6833802645746976, 0.7598740624025577]     [-0.4306282387172724, -0.492823588452425, -0.3448721046225318]
    

    CSV 输出示例:

    X,Y,Z
    "[121.0, 120.0, 121.0]","[0.22348772580529142, 0.5318157129606311, 0.036835975745886484]","[-0.4518270760155507, -0.17152996183789737, -0.40784013453270407]"
    "[120.0, 121.0, 119.0]","[0.5318157129606311, 0.036835975745886484, -0.4063989968475561]","[-0.17152996183789737, -0.40784013453270407, 0.25757581775896665]"
    "[121.0, 119.0, 120.0]","[0.036835975745886484, -0.4063989968475561, -0.6245575426774967]","[-0.40784013453270407, 0.25757581775896665, 0.1596756203790901]"
    "[119.0, 120.0, 120.0]","[-0.4063989968475561, -0.6245575426774967, -0.8385174624702503]","[0.25757581775896665, 0.1596756203790901, 0.19985839324997512]"
    "[120.0, 120.0, 118.0]","[-0.6245575426774967, -0.8385174624702503, 0.47688059239794]","[0.1596756203790901, 0.19985839324997512, -0.4683617649289855]"
    "[120.0, 118.0, 119.0]","[-0.8385174624702503, 0.47688059239794, -0.11738155420809382]","[0.19985839324997512, -0.4683617649289855, -0.4306282387172724]"
    "[118.0, 119.0, 118.0]","[0.47688059239794, -0.11738155420809382, -0.6833802645746976]","[-0.4683617649289855, -0.4306282387172724, -0.492823588452425]"
    "[119.0, 118.0, 118.0]","[-0.11738155420809382, -0.6833802645746976, 0.7598740624025577]","[-0.4306282387172724, -0.492823588452425, -0.3448721046225318]"
    

    【讨论】:

    • 运行此程序时出现构造错误。 "ValueError: 传递值的形状是 (1, 8),索引意味着 (3, 8)"
    • 你用的是什么版本?此外,您在提供的示例数据帧上收到此错误,或者您已将不同的数据帧传递给窗口函数?
    猜你喜欢
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 2011-12-20
    相关资源
    最近更新 更多