假设我要管理许多股票经纪帐户,每个帐户中都有不同类型的股票。我正在尝试编写一些代码来执行压力测试。
我想做的是,我有2个数据框:
帐户信息(数据框):
account = {'account':['1', '1', '1', '2', '2'], 'Stock type':['A', 'A', 'B', 'B', 'C'], 'share value' = '100', '150', '200', '175', '85']}
压力测试方案(数据框):
test = {'stock type':['A', 'B', 'C', 'D'], 'stress shock':['0.8', '0.7', '0.75', 0.6']}
鉴于这两个数据框,我想为每个帐户计算压力冲击后的股票价值。
即对于帐户1,在冲击值= 100 * 0.8 + 150 * 0.8 + 200 * 0.7 = 340后
我尝试了一些基本的for循环,但是我的jupyter笔记本将在运行后很快崩溃(内存不足)。
shocked = []
for i in range(len(account)):
for j in range(len(test)):
if account.loc[i,'Stock type'] == test.loc[j,'stock type']:
shocked.append(account.loc[i,'share value']*test.loc[j, 'stock type']
答案 0 :(得分:0)
创建一个1 users
Mark – joined_at 2019-05-01
至map
“股票类型”以“压力冲击”。
然后将pandas.groupby.apply
与Series
函数一起使用以获得期望的结果:
lambda
[输出]
stress_map = test.set_index('stock type')['stress shock']
account.groupby('account').apply(lambda x: (x['Stock type'].map(stress_map) * x['share value']).sum())
答案 1 :(得分:0)
We can first do a merge
to get the data of the two dataframes together. Then we calculate the after shock value
and finally get the sum
of each account
:
merge = account.merge(test, on='Stock type')
merge['after_stress_shock'] = pd.to_numeric(merge['share value']) * pd.to_numeric(merge['stress shock'])
merge.groupby('account')['after_stress_shock'].sum()
account
1 340.00
2 186.25
Name: after_stress_shock, dtype: float64
Note I used pandas.to_numeric
,因为您的值是CGPoint loc = [theEvent locationInNode:self];
类型。