AnswerBun.com

Inserting a new column into a dataframes gives 'ValueError: Length of values (4) does not match length of index (6)'

Stack Overflow Asked by sanane sanane on December 10, 2020

I created a dataframe with the pandas library. I want to add a column to the dataframe. However I am getting the following error.But I think I have to enter as much data as the number of lines.How can I enter information in the row and column I want? How can I create a column without entering data?

import pandas as pd

kd = pd.DataFrame(data) 
insertColumns = kd.insert(0, "Age", [21, 23, 24, 21],True ) 
print(kd)

error:

ValueError: Length of values (4) does not match length of index (6)

2 Answers

Your problem is that you insert too few rows. Your dataframe contains 6 rows, you only add 4 values in your insert statement. If you add a new column with data, it has to match the current length:

import pandas as pd

df = pd.DataFrame({"1":list(range(6))})
df.insert(0, "Age", [21, 23, 24, 21],True )
# Length of values does not match length of index

You can add a new empty column like so:

df["new_col"] = None

# or

df.insert(0,"Age", None, True)  # to get a new colum at position 0 all None

or "trick" pandas by list slicing the given data and append enough Nones at end:

# extend the data you want to give by a None-List and slice the whole down to size
df.insert(0,"Age", ([21,23,24,21] + [None]*len(df))[:len(df)], True)

to get

    Age  1
0  21.0  0
1  23.0  1
2  24.0  2
3  21.0  3
4   NaN  4   # only 2 None appends needed 
5   NaN  5

Correct answer by Patrick Artner on December 10, 2020

this works

kd = pd.DataFrame(data) 
kd["col_name"]=[21, 23, 24, 21]

for this u need to make sure that the length of list is equal to number of rows. if some rows are empty u need to do this

kd["col_name"]=[21,None, 23, 24,None, 21]

for entering information in desired row and column u can do this as below

kd.loc["index_name","col_name"]=value

Answered by navaneeth nanda on December 10, 2020

Add your own answers!

Related Questions

Pygame doesn’t play mp3 files tagged with mutagen

1  Asked on January 14, 2021 by droptop

       

Average by group for ‘n’ number of rows per group – R

2  Asked on January 14, 2021 by codemaster

       

Non consecutive combinations of array elements in R

4  Asked on January 13, 2021 by guest2341

 

How to mutate complex object with nested array with SWR?

1  Asked on January 13, 2021 by geraltdiesocke

   

Why do I not see a text box to add a menu item to a menustrip

1  Asked on January 13, 2021 by justsomeguytn

   

Alembic. ModuleNotFoundError in env.py

2  Asked on January 13, 2021 by mroldsir

     

What type in c# is most like an expression in R and why?

1  Asked on January 12, 2021 by kirsten-greed

     

Difference between two loops

4  Asked on January 12, 2021 by mszros-zoltn

     

How can i do a request an external url?

0  Asked on January 12, 2021 by mikkylekyle

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP