Skip to content
Advertisement

How to fix Value error “Length of Values (1) doesn match length of index (15)”

Workflow =>

  • Read CSV file and get Unit Price column data
  • Convert column data price and create a new column as name ‘Fabric’
  • save the output as xlsx

Sample:

Unit Price
----------
330
350
380

I want to convert this data 

Fabric
------
Card
Combed
Viscos

My code:

##Fabric Data 
getFabric = df_new['Unit Price']
result = []
for fabric in getFabric:
    if fabric == 310:
        result.append("Card")        
    elif fabric == 330:
        result.append("Combed Dawah")
    elif fabric == 350:
        result.append("Combed Regular")
    elif fabric == 490:
        result.append("Viscos")
    elif fabric == 550:
        result.append("Pleated")
    else:
        result.append(fabric)
    df_new['Fabric'] = result

Error :

Advertisement

Answer

Insted of iterating column value. Try this,

pandas built-in function called .replace() is useful for replacing the value in the column without iteration throung it

df_new['Unit Price'].replace({310: 'Card', 330: 'Combed Dawah', 350: 'Combed Regular', 490: 'Viscos', 550: 'Pleated'}, inplace=True)

Above code will successfully relace the dataframe column values inplace.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement