[Python] DataFrame Copy vs. View: Assignment Issue

Discussão em 'Python' iniciado por Stack, Fevereiro 11, 2025.

  1. Stack

    Stack Membro Participativo

    The following code does not change the scores dataframe.

    scores[["Quiz1", "Quiz2"]] = scores.[["Quiz1", "Quiz2"]].bfill(axis=1)


    However, this statement does the change:

    scores.loc[:, ["Quiz1", "Quiz2"]] = scores.loc[:, ["Quiz1", "Quiz2"]].bfill(axis=1)


    And interestingly, the first approach does change the scores in the following statement:

    scores[['Quiz1', 'Quiz2', 'Quiz3']] = scores[['Quiz1', 'Quiz2', 'Quiz3']].astype('int64')


    I think this is about working on the copy or view of the dataframe. But, I feel like there is an inconsistency how pandas dataframe behave. Can someone explain what is happing here?

    Continue reading...

Compartilhe esta Página