简体   繁体   中英

how to replace single string with list of string in data frame column

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM