1

I want to replace single string with list of string in the data frame column. I have tried below code but not able to do. It is only replacing single string.

import pandas as pd
# initialize list of lists 
data = [['tom', 10,'aaaaa'], ['nick', 15,'vvvvv'], ['juli', 14,'sssssss']] 
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Age','sex']) 
replacements = {'aaaaa': ['M','H'],'vvvvv': ['F','L']}
df['new']=df['sex'].replace(replacements)
print(df)

Getting error ValueError: cannot assign mismatch length to masked array. Could you pleae help me to resolve this issue.

1
  • You have to convert the column to either take a list or combine the list elements to a string Commented Jul 29, 2020 at 19:22

1 Answer 1

2

There is one way around this, you could convert your column to list. And if you have the separator fixed, then in that case you could go around it this way.

df.sex = df.sex.apply(lambda x:[x]) # This will convert them to lists
df.sex = df.sex.str[0].replace('aaaaa','M,H').apply(lambda x: x.split(","))

Also, you can replace 'aaaaa' with a list of the items you want to replace like ['aaaaa','vvvvv'] and map it to ['M,H', 'U,F']

This is a hacky way but one way to go about it.

0       [M, H]
1      [vvvvv]
2    [sssssss]

Ex - cols = ['aaaaa','vvvvv'] new_cols = ['M,H', 'F,V']

df.sex = df.sex.str[0].replace(cols,new_cols).apply(lambda x :x.split(','))
0       [M, H]
1       [F, V]
2    [sssssss]
Sign up to request clarification or add additional context in comments.

5 Comments

I want to replace "vvvvv" also with ["F","L"]. how can i do both the rows at a time
Added in the answer, this should work for multiple columns now.
do you have any other way to suggest.
It should work, I got the result as displayed. Could you elaborate regarding the output you are getting
Right now, I don't have any other way but will think about it and reply when I get an idea

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.