在多个文件1 x 1 pandas上执行代码

发布于 2025-01-30 14:06:15 字数 1798 浏览 1 评论 0原文

嗨,我有编写的代码,我写了一个文件夹中的.CSV文件,并添加一些必需的列。 现在,我想在路径文件夹1 x 1中的多个文件上执行此代码,并将每个代码保存为单独的DF。

我当前的代码如下,

    import pandas as pd
    import glob
    import os

    path = r'C:\Users\jake.jennings.BRONCO\Desktop\GPS Reports\Games\Inputs\2022-03-27 Vs 
    Cowboys\Test' # use your path
    all_files = glob.glob(path + "/*.csv")

    li = []

    for filename in all_files:
        frame = pd.read_csv(filename, index_col=None, skiprows=8)
        li.append(frame)

    frame = pd.concat(li, axis=0, ignore_index=True)
    frame['filename'] = os.path.basename

    #Add odometer change and turn all accel values to positive
    import numpy as np
    frame['OdChange'] = frame['Odometer'].diff()
    frame['accelpos'] = frame['Acceleration'].abs()

    #Add column with OdChange @ >5.5m/s
    frame["new1"] = np.where(
       (frame.Velocity >=5.5), 
       frame["OdChange"], 
       '0')

    #Add column with accels/decels >2.5m.s.s for AccelDec/min
    frame["new2"] = np.where(
       (frame.accelpos >=2.5), 
       frame["accelpos"], 
       '0')

    #Add column with accels/decels >2.5m.s.s for AccelDec/min
    frame["new3"] = np.where(
       (frame.Acceleration >=2.5), 
       '1', 
       '0')

    s = frame['new3'].astype(int)
    frame['new4'] = s.diff().fillna(s).eq(1).astype(int)
    frame['new4']

    #m/min peaks
    frame['1minOD'] = frame['OdChange'].rolling(window=600, axis=0).sum()
    #HSm/min peaks
    frame['1minHS'] = frame['new1'].rolling(window=600, axis=0).sum()
    #AccImpulse/min
    frame['1minImp'] = frame['accelpos'].rolling(window=600, axis=0).mean() *60
    #AccDec Peak Count
    frame['1minAccCount'] = frame['new4'].rolling(window=600, axis=0).sum()
    print (frame)

我不确定这是否是做我要做的事情的最佳方法。任何帮助将不胜感激!

Hi I have code I have written to read a .csv file in a folder and add some required columns.
I now want to perform this code on multiple files within the path folder 1 by 1 and save each as a separate df.

My current code is as follows

    import pandas as pd
    import glob
    import os

    path = r'C:\Users\jake.jennings.BRONCO\Desktop\GPS Reports\Games\Inputs\2022-03-27 Vs 
    Cowboys\Test' # use your path
    all_files = glob.glob(path + "/*.csv")

    li = []

    for filename in all_files:
        frame = pd.read_csv(filename, index_col=None, skiprows=8)
        li.append(frame)

    frame = pd.concat(li, axis=0, ignore_index=True)
    frame['filename'] = os.path.basename

    #Add odometer change and turn all accel values to positive
    import numpy as np
    frame['OdChange'] = frame['Odometer'].diff()
    frame['accelpos'] = frame['Acceleration'].abs()

    #Add column with OdChange @ >5.5m/s
    frame["new1"] = np.where(
       (frame.Velocity >=5.5), 
       frame["OdChange"], 
       '0')

    #Add column with accels/decels >2.5m.s.s for AccelDec/min
    frame["new2"] = np.where(
       (frame.accelpos >=2.5), 
       frame["accelpos"], 
       '0')

    #Add column with accels/decels >2.5m.s.s for AccelDec/min
    frame["new3"] = np.where(
       (frame.Acceleration >=2.5), 
       '1', 
       '0')

    s = frame['new3'].astype(int)
    frame['new4'] = s.diff().fillna(s).eq(1).astype(int)
    frame['new4']

    #m/min peaks
    frame['1minOD'] = frame['OdChange'].rolling(window=600, axis=0).sum()
    #HSm/min peaks
    frame['1minHS'] = frame['new1'].rolling(window=600, axis=0).sum()
    #AccImpulse/min
    frame['1minImp'] = frame['accelpos'].rolling(window=600, axis=0).mean() *60
    #AccDec Peak Count
    frame['1minAccCount'] = frame['new4'].rolling(window=600, axis=0).sum()
    print (frame)

I am not sure if this is even the best way to do what I am trying to do. Any help would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文