Python:循环作用于多个文件并编写新文件

时间:2016-04-10 08:04:26

标签: python csv pandas

我有以下代码,其中包含“University2.csv”文件, 并编写新的csv文件“Hours.csv” - “Hours -Stacked.csv”和“Days.csv”。

现在我希望代码能够在几个文件(University3.csv,University4.csv等)上循环和运行,并为每个文件生成“Hours3.csv”,“Hours - Stacked3.csv”“Days3 .csv“,”Hours4.csv“等。

以下是代码:

    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)

有人可以帮忙吗?

谢谢!

3 个答案:

答案 0 :(得分:1)

以下代码可以解决问题。

它使用索引(idx)运行for循环,它使用以下值(3,4,5)

它使用变量文件名,idx作为参数。 例如

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

以下是代码:

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)

答案 1 :(得分:1)

我认为你可以使用循环列表YoutubePlayerFragment。我将文件名中的数字提取到files,然后将它们添加到输出名称。

此外,您可以ireset_index获得df5,而不必再次df3

read_csv

编辑:

更一般的是Yaron solution,我使用它并仅将import pandas as pd files = ['university1.csv','university2.csv','university3.csv'] for f in files: i = f[-5] print i #Importing the csv file into df df = pd.read_csv(f, 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'+ i + '.csv') df4.to_csv('Day'+ i + 's.csv') #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']) df5 = df3.reset_index() 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'+ i + '.csv') 更改为range()

2,3,4

答案 2 :(得分:1)

另一种选择是使用sys模块

这就是你可以通过调用python程序prog.py one.csv two.csv 1..n.csv

来使用它
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sys

for input in sys.argv:
    process(input)

def process(input):
    #Importing the csv file into df
    df = pd.read_csv(input, 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 = input[:-4]+'_Hours.csv'
    df3.to_csv(hours, index=True)  
    df4.to_csv(input[:-4]+'_Days.csv', index=True)    

    #New csv with stacked hour averages
    df5 = pd.read_csv(hours)
    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(input[:-4]+'_Hours - stacked.csv', index=True)