简体   繁体   中英

How to call more than one function in python

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*')

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM