[Python] pandas eval: creating column names with spaces giving odd names

Discussão em 'Python' iniciado por Stack, Outubro 4, 2024.

  1. Stack

    Stack Membro Participativo

    My df contains columns m 1 and n.

    I was trying to create a duplicate of column m 1 as m 2 using eval. Came across this thread which suggested to use backticks (`). So I used

    df2.eval('`m 2` = `m 1`')


    Now this is creating the duplicate column, but the column name is BACKTICK_QUOTED_STRING_m_2 instead of m 2. Backticks for the source m 1 worked fine and it took the correct column; But did not properly work for the target column.

    I did a bit of googling, didn't get anything. Then I asked copilot, it suggested to use assign instead of eval.
    So my query is, is it possible to use eval for the above case?

    Continue reading...

Compartilhe esta Página