I am working with a pandas dataframe with just two columns market_mechanism
and execution_venue
.
e.g.
market_mechanism execution_venue
4 AQXE
5 AQXE
6 AQEU
5 AQEU
When the market mechanism is 5, I would like to map AQXE
-> ABCD
and AQEU
-> DCBA
. Otherwise I would like to leave them as they are.
I want to use a dictionary to do this; I am close but can't quite get it. Here is my code:
trades_df.loc[trades_df['market_mechanism'] == '5',
'execution_venue'] = {'AQXE': 'AQXA', 'AQEU': 'AQEA'}.get(not_sure_what_goes_here)
I know the syntax for dictionary and doing a get
in this case is perfect, but I'm not sure what to insert to refer to the input execution_venue
.
CodePudding user response:
You could try this:
d = {'AQXE' : 'ABCD', 'AQEU' : 'DCBA'}
df['execution_venue'] = df['execution_venue'].mask(df['market_mechanism'].eq(5), df['execution_venue'].map(d))
df
market_mechanism execution_venue
0 4 AQXE
1 5 ABCD
2 6 AQEU
3 5 DCBA
or else similar to your own approach:
df.loc[df['market_mechanism'].eq(5), 'execution_venue'] = df['execution_venue'].map(d)