1

现在我必须对 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

有没有更简单的方法来做到这一点?

干杯!

4

1 回答 1

1

如果我理解正确,您希望 Pandas 等效于 SQL group_by 和聚合函数。它们本质上是相同groupby的,DataFrame 的aggregate方法和groupby.SeriesGroupBy对象的方法。

>>> dataframe_one['ts_economics_atcf'].groupby(level=[0,1]).aggregate(np.irr)
ts_project_id  ts_scenario
1              hgh            0.544954
2              low            0.138952
dtype: float64
于 2013-11-13T19:32:08.470 回答