Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I add conditional logic to fill certain rows of a dataframe with certain strings when certain conditions are met?

I am trying to write some Python logic to fill a csv file/pandas dataframe table called (table) with certain conditions, but I can’t seem to get it to do what I want.

I have two columns in table: 1. trade_type and 2. execution_venue.

Conditional statement I want to write in Python:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The execution_venue entry will only be filled with either AQXE or AQEU, depending on the trade_type.

When the trade_type is filled with the string DARK, I want the the execution_venue to be filled with XUBS (if it was filled with AQXE before), and AQED (if it was filled with AQEU before).

Here is my code to do this:

security_mic = ('AQXE', 'AQEU')
table.loc[table['trade_type'] == 'DARK', 'execution_venue'] = {'AQXE': 'XUBS',
                                                                               'AQEU': 'AQED'}.get(security_mic)

>Solution :

Lets use replace for substitution of old values where trade_type os DARK

d = {'AQXE': 'XUBS', 'AQEU': 'AQED'}
table.loc[table['trade_type'] == 'DARK', 'execution_venue'] = table['execution_venue'].replace(d)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading