Hi, i want to run in a dateframe calculations which include the RIC "EUR=". This causes an error...

Hi, i want to run in a dateframe calculations which include the RIC "EUR=". This causes an error where = is part of the python language. How can I work around this? Below my example df['COALEUR']=df.TRAPI2YZ0/(df.EUR=)

Best Answer

  • Alex Putkov.1
    Alex Putkov.1 ✭✭✭✭✭
    Answer ✓

    Selection using attribute operator in a pandas dataframe may be convenient in some cases, but it's not universally applicable as attribute operator has shortcomings like the one you encountered: it cannot be used when column label contains special characters such as a white space or an equals sign. Use index operator rather than attribute operator:

    df['COALEUR'] = df['TRAPI2YZ0'] / df['EUR=']
    Or if you prefer rename the column, then use attribute operator.
    df.rename(columns={'EUR=': 'EUR'}, inplace=True)
    df['COALEUR'] = df.TRAPI2YZ0 / df.EUR

Answers