我具有以下数据框,我希望添加一个名为 open_next_year 的新列。
将通过比较两列来选择此列; 财政年度 +1和股票行情。 然后使用打开列中的值。
原始数据框:
fiscalYear ticker open
2017 FINL 17.4880
2017 AAPL 17.4880
...
2016 FINL 16.4880
2016 AAPL 16.4880
2015 FINL 15.4880
2015 AAPL 15.4880
所需数据框:
fiscalYear ticker open open_next_year
2017 FINL 17.4880
2017 AAPL 17.4880
2016 FINL 16.4880 17.4880
2016 AAPL 16.4880 17.4880
2015 FINL 15.4880 16.4880
2015 AAPL 15.4880 16.4880
请问熊猫实现这一目标的方法是什么?
答案 0 :(得分:2)
我相信每个组都需要将所有值都移动DataFrameGroupBy.shift
:
df['open_next_year'] = df.groupby('ticker')['open'].shift()
print (df)
fiscalYear ticker open open_next_year
0 2017 FINL 17.488 NaN
1 2017 AAPL 17.488 NaN
2 2016 FINL 16.488 17.488
3 2016 AAPL 16.488 17.488
4 2015 FINL 15.488 16.488
5 2015 AAPL 15.488 16.488
更改了唯一open
值的示例:
print (df)
fiscalYear ticker open
0 2017 FINL 17.4881
1 2017 AAPL 17.4882
2 2016 FINL 16.4883
3 2016 AAPL 16.4884
4 2015 FINL 15.4885
5 2015 AAPL 15.4886
df['open_next_year'] = df.groupby('ticker')['open'].shift()
print (df)
fiscalYear ticker open open_next_year
0 2017 FINL 17.4881 NaN
1 2017 AAPL 17.4882 NaN
2 2016 FINL 16.4883 17.4881
3 2016 AAPL 16.4884 17.4882
4 2015 FINL 15.4885 16.4883
5 2015 AAPL 15.4886 16.4884
答案 1 :(得分:0)
这是另一种先创建地图的方法。
m = dict(zip(tuple(zip(df.fiscalYear - 1, df.ticker)),df.open))
df['open_next_year'] = df[['fiscalYear','ticker']].apply(tuple, 1).map(m)
地图/字典看起来像这样,并且是通过 zip 一起在第1年,股票代码和开盘价中获得的:
{(2014, 'AAPL'): 15.488,
(2014, 'FINL'): 15.488,
(2015, 'AAPL'): 16.488,
(2015, 'FINL'): 16.488,
(2016, 'AAPL'): 17.488,
(2016, 'FINL'): 17.488}
完整示例:
data = '''\
fiscalYear ticker open
2017 FINL 17.488
2017 AAPL 17.488
2016 FINL 16.488
2016 AAPL 16.488
2015 FINL 15.488
2015 AAPL 15.488'''
fileobj = pd.compat.StringIO(data)
df = pd.read_csv(fileobj, sep='\s+')
m = dict(zip(tuple(zip(df.fiscalYear - 1, df.ticker)),df.open))
df['open_next_year'] = df[['fiscalYear','ticker']].apply(tuple, 1).map(m)
print(df)
返回:
fiscalYear ticker open open_next_year
0 2017 FINL 17.488 NaN
1 2017 AAPL 17.488 NaN
2 2016 FINL 16.488 17.488
3 2016 AAPL 16.488 17.488
4 2015 FINL 15.488 16.488
5 2015 AAPL 15.488 16.488