14

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

2

4 Answers 4

21

use

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

or as suggested by @Wen use

df3 = df3.append(data)
Sign up to request clarification or add additional context in comments.

2 Comments

axis=0 is a default value and can be omitted
If you do not use the ignore_index then maybe will appear a error. df3 = df3.append(data, ignore_index=True )
12

From https://pandas.pydata.org/pandas-docs/stable/merging.html:

It is worth noting however, that concat (and therefore append) makes a full copy of the data, and that constantly reusing this function can create a significant performance hit. If you need to use the operation over several datasets, use a list comprehension.

You should use loc, like you were trying to do, and with a dictionary where the keys are the column names and the values are the data of the row being added.

import pandas as pd

df3 = pd.DataFrame(columns=["Devices","months"])
new_entry = {'Devices': 'device1', 'months': 'month1'}

df3.loc[len(df3)] = new_entry

1 Comment

how to append a dataframe to an existing dataframe using the above technique?
1

If someone is looking to append new row which is in dictionary format, below will help.

  • Existing DataFrame
In [6]: df
Out[6]: 
     Devices     months
0  Powerbank  Feb month

In [7]:
  • Below snippet adds another row to existing DataFrame.
In [7]: dictionary_row = {"Devices":"Laptop","months":"Mar month"}

In [8]: df = df.append(dictionary_row, ignore_index=True)

In [9]: df
Out[9]: 
     Devices     months
0  Powerbank  Feb month
1     Laptop  Mar month

In [10]:

Hope that helps.

Comments

0

As the error suggests the number of columns should of the data being inserted into the dataframe must match the number of columns of the dataframe

>>> df3=pd.DataFrame(columns=["Devices","months"])
>>> df3.loc[len(df3)] = ['Powerbank','Feb']
>>> df3
     Devices months
0  Powerbank    Feb
>>> data = ['powerbank','feb']
>>> df3.loc[len(df3)] = data
>>> df3
     Devices months
0  Powerbank    Feb
1  powerbank    feb

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.