Reputation: 95
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.
Upvotes: 1
Views: 166
Reputation: 1225
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]
Upvotes: 2