for some reason when I call all four functions at once I get an error with the newly named dataframes. specifically the empty dataframes that I want to fill. Have no idea why. I've tried to move all empty dataframes outside the function and that didn't work. Any help appreciated.
The first function works (FID_extract1_to_9) , but the final three do not.
The error:
new_dfa[colname] = selected_data
NameError: global name 'new_dfa' is not defined
import glob
import pandas as pd
import os
os.chdir('C:/Users/Joey/Desktop/GC_results')
def FID_extract1_to_9(filepath):
path_pattern = filepath
files = glob.glob(path_pattern) #finds all files with ending in 00* in the file path
dataframes = [pd.DataFrame.from_csv(f, index_col=None) for f in files]
new_df = pd.DataFrame()
for i, df in enumerate(dataframes):
colname = 'Run {}'.format(i+1)
selected_data = df['Unnamed: 3'].ix[12:16]
new_df[colname] = selected_data
print new_df
new_df.to_csv('FID_11169_liquid.csv') #Enter name of output file here
def FID_extract9_to_96(filepath):
path_pattern = filepath
files = glob.glob(path_pattern)
dataframes = [pd.DataFrame.from_csv(f, index_col=None) for f in files]
new_dfa = pd.DataFrame()
for i, df in enumerate(dataframes):
colname = 'Run {}'.format(i+1)
selected_data = df['Unnamed: 3'].ix[12:16]
new_dfa[colname] = selected_data
print new_dfa
new_dfa.to_csv('FID_11169_Liquid.csv')
def TCD_extract1_to_9(filepath):
path_pattern = filepath
files = glob.glob(path_pattern)
dataframes = [pd.DataFrame.from_csv(f, index_col=None) for f in files]
new_dfb = pd.DataFrame()
for i, df in enumerate(dataframes):
colname = 'Run {}'.format(i+1)
selected_data = df['Unnamed: 3'].ix[12:16]
new_df[colname] = selected_data
print new_dfb
new_dfb.to_csv('TCD_11169_liquid.csv')
def TCD_extract9_to_96(filepath):
path_pattern = filepath
files = glob.glob(path_pattern)
dataframes = [pd.DataFrame.from_csv(f, index_col=None) for f in files]
new_dfc = pd.DataFrame()
for i, df in enumerate(dataframes):
colname = 'Run {}'.format(i+1)
selected_data = df['Unnamed: 3'].ix[12:16]
new_dfa[colname] = selected_data
print new_dfc
new_dfc.to_csv('TCD_11169_Liquid.csv')
FID_extract1_to_9('C:/Users/Joey/Desktop/Cryostat Verification/GC results/11169_Cryo_1bar/FID_00*') #files directory
FID_extract9_to_96('C:/Users/Joey/Desktop/Cryostat Verification/GC results/11169_Cryo_1bar/FID_0*')
TCD_extract1_to_9('C:/Users/Joey/Desktop/Cryostat Verification/GC results/11169_Cryo_1bar/TCD_00*')
TCD_extract9_to_96('C:/Users/Joey/Desktop/Cryostat Verification/GC results/11169_Cryo_1bar/TCD_0*')
new_dfbbut you usenew_dfand in the last one it'snew_dfcbut you usenew_dfaconcatpotentially