Python: Loop acting on several files and writing new ones
Question: I have the following code which takes the file "University2.csv", and writes
new csv files "Hours.csv" - "Hours -Stacked.csv" and "Days.csv".

Now I want the code to be able to loop and run on several files
(University3.csv, University4.csv etc.) and produce for each of them
"Hours3.csv", "Hours - Stacked3.csv" "Days3.csv", "Hours4.csv" etc.

Here is the code:

    
    
        import pandas as pd
        import numpy as np
        import matplotlib.pyplot as plt
    
    
    #Importing the csv file into df
    df = pd.read_csv('university2.csv', sep=";", skiprows=1)
    
    #Changing datetime
    df['YYYY-MO-DD HH-MI-SS_SSS'] = pd.to_datetime(df['YYYY-MO-DD HH-MI-SS_SSS'], 
                                                   format='%Y-%m-%d %H:%M:%S:%f')
    
    #Set index from column
    df = df.set_index('YYYY-MO-DD HH-MI-SS_SSS')
    
    #Add Magnetic Magnitude Column
    df['magnetic_mag'] = np.sqrt(df['MAGNETIC FIELD X (μT)']**2 + df['MAGNETIC FIELD Y (μT)']**2 + df['MAGNETIC FIELD Z (μT)']**2)
    
    #Copy interesting values
    df2 = df[[ 'ATMOSPHERIC PRESSURE (hPa)',
              'TEMPERATURE (C)', 'magnetic_mag']].copy()
    
    #Hourly Average and Standard Deviation for interesting values 
    df3 = df2.resample('H').agg(['mean','std'])
    df3.columns = [' '.join(col) for col in df3.columns]   
    
    #Daily Average and Standard Deviation for interesting values 
    df4 = df2.resample('D').agg(['mean','std'])
    df4.columns = [' '.join(col) for col in df4.columns] 
    
    #Write to new csv
    df3.to_csv('Hours.csv', index=True)  
    df4.to_csv('Days.csv', index=True)    
    
    #New csv with stacked hour averages
    df5 = pd.read_csv('Hours.csv')
    df5['YYYY-MO-DD HH-MI-SS_SSS'] = pd.to_datetime(df5['YYYY-MO-DD HH-MI-SS_SSS'])  
    hour = pd.to_timedelta(df5['YYYY-MO-DD HH-MI-SS_SSS'].dt.hour, unit='H')
    df6 = df5.groupby(hour).mean()
    df6.to_csv('Hours - stacked.csv', index=True)
    

Can anyone help ?

Thank you !


Answer: The following code should do the trick.

It runs a for loop using index (idx) which uses the following values (3,4,5)

It use variable filenames, with the idx as parameter. e.g.

    
    
    uni_name = "university" + str(idx) + ".csv"
    

Here is the code:

    
    
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    for idx in 3,4,5:
        #Importing the csv file into df
        uni_name = "university" + str(idx) + ".csv"
        df = pd.read_csv(uni_name, sep=";", skiprows=1)
    
        #Changing datetime
        df['YYYY-MO-DD HH-MI-SS_SSS'] = pd.to_datetime(df['YYYY-MO-DD HH-MI-SS_SSS'], 
                                                       format='%Y-%m-%d %H:%M:%S:%f')
    
        #Set index from column
        df = df.set_index('YYYY-MO-DD HH-MI-SS_SSS')
    
        #Add Magnetic Magnitude Column
        df['magnetic_mag'] = np.sqrt(df['MAGNETIC FIELD X (μT)']**2 + df['MAGNETIC FIELD Y (μT)']**2 + df['MAGNETIC FIELD Z (μT)']**2)
    
        #Copy interesting values
        df2 = df[[ 'ATMOSPHERIC PRESSURE (hPa)',
                  'TEMPERATURE (C)', 'magnetic_mag']].copy()
    
        #Hourly Average and Standard Deviation for interesting values 
        df3 = df2.resample('H').agg(['mean','std'])
        df3.columns = [' '.join(col) for col in df3.columns]   
    
        #Daily Average and Standard Deviation for interesting values 
        df4 = df2.resample('D').agg(['mean','std'])
        df4.columns = [' '.join(col) for col in df4.columns] 
    
        #Write to new csv
        hours = "Hours" + str(idx) + ".csv"
        days = "Days" + str(idx) + ".csv"
    
        df3.to_csv(hours, index=True)  
        df4.to_csv(days, index=True)    
    
        #New csv with stacked hour averages
        df5 = pd.read_csv('Hours.csv')
        df5['YYYY-MO-DD HH-MI-SS_SSS'] = pd.to_datetime(df5['YYYY-MO-DD HH-MI-SS_SSS'])  
        hour = pd.to_timedelta(df5['YYYY-MO-DD HH-MI-SS_SSS'].dt.hour, unit='H')
        df6 = df5.groupby(hour).mean()
        hours_st = "Hours - stacked" + str(idx) + ".csv"
        df6.to_csv('Hours - stacked.csv', index=True)
    

