现在我不得不对dataframe_one进行计算,然后在dataframe_two上创建一个新列并填充结果。 dataframe_one是多索引的,而第二个不是,但有些列与dataframe_one中的索引匹配。
这就是我目前正在做的事情: 将pandas导入为pd 导入numpy为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
有更简单的方法吗?
干杯!