Getting error when adding a new row to my existing dataframe in pandas

pyco picture pyco · Jul 14, 2017 · Viewed 23.9k times · Source

I have the below data frame.

df3=pd.DataFrame(columns=["Devices","months"])

I am getting row value from a loop row, print(data)

    Devices     months
1  Powerbank  Feb month

When I am adding this data row to my df3 I am getting an error.

  df3.loc[len(df3)]=data

ValueError: cannot set a row with mismatched columns

Answer

muon picture muon · Jul 14, 2017

use

df3 = pd.concat([df3, data], axis=0)

or as suggested by @Wen use

df3 = df3.append(data)