现在我必须对 dataframe_one 进行计算,然后在 dataframe_two 上创建一个新列并填充结果。dataframe_one 是多索引的,而第二个不是,但是有与 dataframe_one 中的索引匹配的列。
这就是我目前正在做的事情: import pandas as pd import numpy as np
dataframe_two = {}
dataframe_two['project_id'] = [1, 2]
dataframe_two['scenario'] = ['hgh', 'low']
dataframe_two = pd.DataFrame(dataframe_two)
dataframe_one = {}
dataframe_one['ts_project_id'] = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2]
dataframe_one['ts_scenario'] = ['hgh', 'hgh', 'hgh', 'hgh', 'hgh', 'low', 'low', 'low', 'low', 'low']
dataframe_one['ts_economics_atcf'] = [-2, 2, -3, 4, 5 , -6, 3, -3, 4, 5]
dataframe_one = pd.DataFrame(dataframe_one)
dataframe_one.index = [dataframe_one['ts_project_id'], dataframe_one['ts_scenario']]
project_scenario = zip(dataframe_two['project_id'], dataframe_two['scenario'])
dataframe_two['econ_irr'] = np.zeros(len(dataframe_two.index))
i = 0
for project, scenario in project_scenario:
# Grabs corresponding series from dataframe_one
atcf = dataframe_one.ix[project].ix[scenario]['ts_economics_atcf']
irr = np.irr(atcf.values)
dataframe_two['econ_irr'][i] = irr
i = i + 1
print dataframe_two
有没有更简单的方法来做到这一点?
干杯!