Adam Skluzáček
Adam Skluzáček

Reputation: 43

Condition from a string to select dataframe rows

I have a dataframe like this:

data = [[1, 0, 1],
        [1, 1, 1],
        [0, 0, 1]]

df = pd.DataFrame(data=data, columns=['col1', 'col2', 'col3'])

and from a string like this:

cond_str = ['col1 >= 1', 'col3 >= 1']

and I would like to achieve this:

df.loc[(df['col1'] >= 1) & (df['col3'] >= 1)]

Is there any simple way to do this or do I have to write my own parsing function for this?

Upvotes: 4

Views: 502

Answers (2)

Saurabh Kansal
Saurabh Kansal

Reputation: 702

you can use eval of python to evaluate a string expression. e.g. for your case

df = pd.DataFrame(data=data, columns=['col1', 'col2', 'col3'])
print(df)

    col1  col2  col3
0     1     0     1
1     1     1     1
2     0     0     1

cond_str_list = ['col1 >= 1', 'col3 >= 1']

cond_str = ' & '.join(['(df.'+i+')' for i in  cond_str_list])

print(cond_str)

df = df[eval(cond_str)]

print(df)

    col1  col2  col3
0     1     0     1
1     1     1     1

Upvotes: 0

jmkjaer
jmkjaer

Reputation: 1098

You can use query:

df.query("&".join(cond_str))

which results in

   col1  col2  col3
0     1     0     1
1     1     1     1

Upvotes: 5

Related Questions