![](/img/trans.png)
[英]How are functions called in a Recursive function when there are more than 1 function call in one statement in Python?
[英]How to call more than one function in python
由於某種原因,當我一次調用所有四個函數時,新命名的數據幀出現錯誤。 特別是我要填充的空數據框。 不知道為什么。 我試圖將所有空的數據框移到函數外部,但這沒有用。 任何幫助表示贊賞。
第一個函數有效(FID_extract1_to_9),但最后三個不起作用。
錯誤:
new_dfa [colname] = selected_data
NameError:全局名稱“ new_dfa”未定義
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*')
您的函數def TCD_extract1_to_9(filepath)
有一個基本語法錯誤,您聲明new_dfb = pd.DataFrame()
但隨后使用new_df[colname] = selected_data
。
在最后一個函數def TCD_extract9_to_96(filepath)
您聲明new_dfc = pd.DataFrame()
,然后使用new_dfa[colname] = selected_data
。
因此,您需要更正此問題。
只是一個建議,如果您的輸出將是csv,則您的代碼看起來很復雜。 只需使用csv模塊,導入csv。
with open("path to save", "wb") as f:
writer = csv.writer(f)
writer.writerows("the data")
這很容易,並且不會出錯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.