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

Apply two different formulas based on the cell content of a dataframe

i have this dataframe named optiondf:

     strike  type settle openInterest volume
0     10000  Call  24'24        0      0
1     15000  Call  0            0      0
2     20000  Call  23'56        0      0
3     25000  Call  1            0      0
4     30000  Call  23'24        0      0

I would like to be able to do some operations on the "settle" column.
I would like to filter based on whether or not the symbol " ‘ " is present in the value in the column.

In case contains " ‘ " putting this calculation as the value of the cell, which I have already verified, has no problems in calculating itself

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

pd.to_numeric(optiondf['settle'].str.split(pat="'").str[0])+pd.to_numeric(optiondf['settle'].str.split(pat="'").str[-1])*(1/64)

In case not contains " ‘ " I would just like to convert the value to a number, like this:

optiondf['settle']=pd.to_numeric(optiondf['settle'])

I have tried with this pandas feature but without success, I probably am doing something wrong:

optiondf['settle'].str.contains("'")=....

>Solution :

You could construct a lambda that applies to different functions:

def func_a(x):
    v = x.split("'")
    return v[0] + v[1] / 64

optiondf['settle'] = optiondf['settle'].apply(lambda x: func_a(x) if "'" in x else pd.to_numeric(x))
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