【问题标题】:Reading in multiple files in Python and saving them one by one in a different directory在 Python 中读入多个文件并将它们一个一个保存在不同的目录中
【发布时间】:2020-08-19 09:18:55
【问题描述】:
    import glob
    import pandas as pd
    import seaborn as sns
    import numpy as np
    from scipy import signal
    import matplotlib.pyplot as plt
    
    files = glob.glob("Angular_position_*_*.csv")
    output = pd.DataFrame()
    
    for f in files:
        df = pd.read_csv(f)
        time = df.iloc[:,0]
        time = time.to_numpy()
        ynew = df.iloc[:,1:]
        ynew = ynew.to_numpy()
        
    
        lowPassCutoffFreq = 6.0 # Cut off frequency
        Sample_freq = 150; #Target sample frequency
        N = 2 # Order of the filter; In this case 2nd order
        Wn = lowPassCutoffFreq/(Sample_freq/2)  #Normalize frequency
    
        b, a = signal.butter(5, Wn, btype='low',analog=False,output='ba')
        #scipy.signal.butter(N, Wn, btype='low', analog=False, output='ba', fs=None)
    
        output = signal.filtfilt(b, a, ynew, axis=0)
        
     
        np.savetxt("enter directory path/Filtered_files/Filtered_Angular_position_*_*", output,   delimiter = ', ', newline = "\n")

我正在尝试读取目录中的所有文件,然后对它们进行低通滤波。之后,结果将一个接一个地保存,但不会保存在一个文件中。结果为每个文件提供了 3 列,理想情况下,我希望它们用标题命名,例如col1,col2,col3。

不使用 glob,我可以单独过滤所有文件,但我有 100 多个这样的文件。

任何帮助将不胜感激。

祝你好运,

【问题讨论】:

    标签: python-3.x pandas numpy glob lowpass-filter


    【解决方案1】:

    除了标题名称之外,我已经部分解决了这个问题:

    import glob
    import pandas as pd
    from tnorma import tnorma
    import seaborn as sns
    import numpy as np
    from scipy import signal
    import matplotlib.pyplot as plt
    
    path = r'location_of_dir'
    
    all_files = glob.glob(path + '/*.csv')
    # yn = np.zeros(shape = (101,1))
    # tn = np.zeros(shape = (101,1))
    #ynew = []
    
    yn = np.zeros(shape = (101,1))
    
    for filename in all_files:
        df = pd.read_csv(filename, index_col=None, header=0)
        print(filename)
        foo = filename.split("/")[-1]
        #df = pd.read_csv(f)
        time = df.iloc[:,0]
        time = time.to_numpy()
        ynew = df.iloc[:,1:]
        ynew = ynew.to_numpy()
        #print(ynew)
    
        lowPassCutoffFreq = 6.0 # Cut off frequency
        Sample_freq = 150; #Target sample frequency
        N = 2 # Order of the filter; In this case 2nd order
        Wn = lowPassCutoffFreq/(Sample_freq/2)  #Normalize frequency
    
        b, a = signal.butter(5, Wn, btype='low',analog=False,output='ba')
        #scipy.signal.butter(N, Wn, btype='low', analog=False, output='ba', fs=None)
    
        output = signal.filtfilt(b, a, ynew, axis=0)
        #print (output)
        tn = np.linspace(0, 100, 101)     # new time vector for the new time-normalized data
        yn, tn, indie = tnorma(output, k=3, smooth =1, mask = None, show = False)
        
        np.savetxt("path_name/foldername/file"+ foo, yn, delimiter = ', ', newline = "\n")
    

    但是,我很难将标题名称放在每个文件的 3 列中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-01
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 2019-07-27
      • 2017-02-15
      • 2016-05-27
      • 1970-01-01
      相关资源
      最近更新 更多