Вы можете использовать пользовательскую функцию с помощью GroupBy.apply
с next
и iter
для получения первого значения и, если нет совпадения, получите NaN
s:
def f(x):
c = next(iter(x.loc[x.Type == "call", 'Price']),np.nan)
p = next(iter(x.loc[x.Type == "put", 'Price']),np.nan)
x['new']= c - p + x.Strike
return x
df = df.groupby(["Expiration","Strike"]).apply(f)
print (df)
Type Price Expiration Strike new
0 put 145.000000 2021-01-15 420.0 NaN
1 call 15.370001 2018-11-30 262.0 275.875001
2 put 1.495000 2018-11-30 262.0 275.875001
3 call 14.930000 2018-11-30 262.5 NaN
Другойрешение:
#if possible `call` and `put` are not unique per groups
c = df[df.Type == "call"].groupby(["Expiration","Strike"])['Price'].first()
p = df[df.Type == "put"].groupby(["Expiration","Strike"])['Price'].first()
#if `call` and `put` are unique per groups
#c = df[df.Type == "call"].set_index(["Expiration","Strike"])['Price']
#p = df[df.Type == "put"].set_index(["Expiration","Strike"])['Price']
df1 = df.join((c - p).rename('new'), on=["Expiration","Strike"])
df1['new'] += df1['Strike']
print (df1)
Type Price Expiration Strike new
0 put 145.000000 2021-01-15 420.0 NaN
1 call 15.370001 2018-11-30 262.0 275.875001
2 put 1.495000 2018-11-30 262.0 275.875001
3 call 14.930000 2018-11-30 262.5 NaN