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

Even defining object before calling query for a DataFrame, returns the error name is not defined

def fl_base(df,file_name):
    columns = ['historic_odds_1','historic_odds_2','historic_odds_3','historic_odds_4','odds']
    mean_cols = df[columns].mean(axis=1)
    df = df.query(
        f"\
            (mean_cols > 0) and \
                ((@df['minute_traded']/mean_cols)*100 >= 1000) and \
                    (@df['minute_traded'] >= 1000)\
                        "
    ).reset_index(drop=True)
    return df
name 'mean_cols' is not defined

If mean_cols is being created before calling df.query, why is it saying that it has not defined?

>Solution :

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 query documentation states:

You can refer to variables in the environment by prefixing them with an ‘@’ character like @a + b.

Here, mean_cols is your variable and not a column name in df.

df = df.query(
        f"\
            (@mean_cols > 0) and \
                ((minute_traded/@mean_cols)*100 >= 1000) and \
                    (minute_traded >= 1000)\
                        "
    ).reset_index(drop=True)

I believe minute_traded is the column of df and mean_cols as stated earlier is the variable.

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