0

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*')
6
  • 2
    But in your last 2 functions the local temp df names are new_dfb but you use new_df and in the last one it's new_dfc but you use new_dfa Commented Jun 11, 2015 at 13:23
  • Ah, my error. Need to pay more attention. Many thanks! :) Commented Jun 11, 2015 at 13:28
  • Many i ask a quick question. I have 96 columns, But i want columns of 12... so effectively 8 rows with columns of 12. Is this possible? Commented Jun 11, 2015 at 13:30
  • 1
    It depends, it should be a separate question, you could use concat potentially Commented Jun 11, 2015 at 13:31
  • 1
    Well your dfs are local to the functions but you then return them which means you need to append each to a list and then you can concat. If it's not this then it should be a separate question after you accept an answer here Commented Jun 11, 2015 at 14:05

2 Answers 2

3

You have a basic syntax error in your function def TCD_extract1_to_9(filepath) you declare new_dfb = pd.DataFrame() but you then use new_df[colname] = selected_data.

In your last function def TCD_extract9_to_96(filepath) you declare new_dfc = pd.DataFrame() but then use new_dfa[colname] = selected_data.

So you need to correct this.

Sign up to request clarification or add additional context in comments.

Comments

0

just a suggestion , If your output is going to be csv, then your code looks complicated. Just use the csv module, import csv.

with open("path to save", "wb") as f:
writer = csv.writer(f)
writer.writerows("the data")

this is easy and will fetch u no errors.

1 Comment

The OP is performing some calculations on the dataframe and then outputting to csv, your suggestion doesn't address this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.