0

I am tring to replace a list value with another list value in a pandas but it raises an error : cannot set using a multi-index selection indexer with a different length than the value. Is it wrong practice to work with list values in pandas? Thank you

import pandas as pd

cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
        'Price': [[123, 123123],[123, 123123],[123, 123123],[123, 123123]]
        }
df = pd.DataFrame(cars, columns = ['Brand', 'Price'])
df[1, 'Price'] = [2342, 23423]

print(df)

2 Answers 2

1

Use DataFrame.loc:

df.loc[1, 'Price'] = [2342, 23423]

print(df)
0     Honda Civic  [123, 123123]
1  Toyota Corolla  [2342, 23423]
2      Ford Focus  [123, 123123]
3         Audi A4  [123, 123123]

Is it wrong practice to work with list values in pandas?

I think working with lists in pandas is not good idea, if possible create 2 columns here.

Sign up to request clarification or add additional context in comments.

Comments

0

You better create two columns of out your Price column like this. But if you still have a good reason to have a list in the Price column and want to update it, use below code:

df.iloc[1, 1] = [2342, 23423]

This is integer-location based indexing for selection. 'Price' is in the index=1 position column wise.So, the 2nd 1 refers to 'Price' column and the 1st 1 refers to the actual row index.

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.