Thursday, March 3, 2022

Pandas DataFrame Filtering Using eval()

import pandas as pd

df = pd.DataFrame({
    "col1": [1, 2, 3, 1, 2, 3],
    "col2": ["A", "B", "A", "B", "A", "B"]
})

c = r"(df['col1'] == 1) | (df['col2'] == 'A')" df[eval(c)]
df[df['col1'] == 1]
d = "df['col1'] == 1" df[eval(d)]
Tags: Technology,Machine Learning,Data Visualization,

No comments:

Post a Comment