[Python] python pandas "cannot set a row with mismatched columns" error

Discussão em 'Python' iniciado por Stack, Agosto 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am facing an issue while adding a new row to the data set.

    Here is the example DataFrame.

    column_names = ['A','B','C']
    items = [['a1','b1','c1'],['a2','b2']]
    newDF = pd.DataFrame(items,columns=column_names)
    print(newDF)


    output:

    A B C
    0 a1 b1 c1
    1 a2 b2 None


    Since c2 was missing, it was replaced with None. This is fine and as expected.

    Now if i continue to add similar rows to this existing DataFrame, like this:

    newDF.loc[len(newDF)] = ['a3','b3']


    I get the error "cannot set a row with mismatched columns".

    How can I add this additional row, so that it will automatically take care of missing c3 with None or NaN?

    Continue reading...

Compartilhe esta Página