2016-11-07 3 views
0

Возможно, простой вопрос - глядя на приведенный ниже код, как я могу повторно записать приведенный ниже образец, чтобы избежать дублирования?Выполнение одной и той же операции с несколькими входами функций

Входы представляют собой кадры данных pandas, которые содержат разные значения под тем же именем столбца - в основном измерения одного и того же события от разных наблюдателей. Я хотел бы сократить длину этой функции (и другие, где у меня есть аналогичное дублирование - не поставлено) пополам.

Это похоже на то, как использовать цикл for, но я не уверен, как/если я могу реализовать его для итерации через входные функции? Я ожидаю, что это простой ответ, но я не смог точно настроить поисковые запросы Google, чтобы выявить ответ сам. Надеюсь, ты поможешь!

def data_manipulation(a, b): 
""" 
Performs math operations on the values contained in trajectory1(and 2). The raw values contained there are converted 
to more useful forms: e.g. apparent to absolute magnitude. These new results are added to the pandas data frame. 
Unnecessary data columns are deleted. 

All equations/constants are lifted from read_data_JETS. 

To Do: - remove code duplication where the same operation in written twice (once for each station). 

:param a: Pandas Data Frame containing values from first station. 
:param b: Pandas Data Frame containing values from second station. 
:return: 
""" 

# Convert apparent magnitude ('Bright') to absolute magnitude (Abs_Mag). 
a['Abs_Mag'] = (a['Bright'] + 2.5 * 
       np.log10((100 ** 2)/((a['dist']/1000) ** 2))) - 0.25 
b['Abs_Mag'] = (b['Bright'] + 2.5 * 
       np.log10((100 ** 2)/((b['dist']/1000) ** 2))) - 0.25 

# Calculate the error in absolute magnitude where 1 is the error in apparent magnitude and 0.001(km) is the error 
# in distance ('dist'). 
a['Abs_Mag_Err'] = 1 + (5/(a['dist']/1000) * 0.001) 
b['Abs_Mag_Err'] = 1 + (5/(b['dist']/1000) * 0.001) 

# Calculate the meteor luminosity from absolute magnitude. 
a['Luminosity'] = 525 * 10 ** (-0.4 * a['Abs_Mag']) 
b['Luminosity'] = 525 * 10 ** (-0.4 * b['Abs_Mag']) 

# Calculate the error in luminosity. 
a['Luminosity_Err'] = abs(-0.4 * a['Luminosity'] * np.log(10) * a['Abs_Mag_Err']) 
b['Luminosity_Err'] = abs(-0.4 * b['Luminosity'] * np.log(10) * b['Abs_Mag_Err']) 

# Calculate the integrated luminosity of each meteor for both stations. 
a['Intg_Luminosity'] = a['Luminosity'].sum() * 0.04 
b['Intg_Luminosity'] = b['Luminosity'].sum() * 0.04 

# Delete column containing apparent magnitude. 
del a['Bright'] 
del b['Bright'] 
+0

Просто передайте один словарь функции, и вызовите функцию дважды (один раз для каждого словаря). –

+0

Очень приятно, спасибо! –

ответ

1

Pass один словарь функции, а затем вызвать его один раз для каждого словаря:

def data_manipulation(x): 
    x['Abs_Mag'] = (x['Bright'] + 2.5 * np.log10((100 ** 2)/((x['dist']/1000) ** 2))) - 0.25 
    x['Abs_Mag_Err'] = 1 + (5/(x['dist']/1000) * 0.001) 
    x['Luminosity'] = 525 * 10 ** (-0.4 * x['Abs_Mag']) 
    x['Luminosity_Err'] = abs(-0.4 * x['Luminosity'] * np.log(10) * x['Abs_Mag_Err']) 
    x['Intg_Luminosity'] = x['Luminosity'].sum() * 0.04 
    del x['Bright'] 

data_manipulation(a) 
data_manipulation(b) 
Смежные вопросы