Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In Pandas, how to replace list elements in Series?

I have a following series and would like to replace a1 with a, b1 with b and c1 with c.

data = pd.Series([['a1', 'b1', 'c1'], ['b1', 'a1', 'c1'], ['c1', 'a1' ,'b1']])

Out[132]: 
0    [a1, b1, c1]
1    [b1, a1, c1]
2    [c1, a1, b1]
dtype: object

The expected results is as below.

0    [a, b, c]
1    [b, a, c]
2    [c, a, b]
dtype: object

The following code does what I am trying to do, but it does not seem to be a nice way to do.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for i, s in enumerate(data):
    temp = ['a' if x == 'a1' else x for x in s]
    temp = ['b' if x == 'b1' else x for x in temp]
    temp = ['c' if x == 'c1' else x for x in temp]
    data.iloc[i] = temp

Is there a better way of doing this? I assume pandas have a built-in function for this.

I tried it with replace, but it does not help.

data.replace['a1', 'a']
data.replace['b1', 'c']
data.replace['c1', 'c']

Thank you for any comment in advance.

>Solution :

Create dictionary for replace and use list comprehension with get – second parameter y is if not exist key get original:

d = {'a1':'a', 'b1':'b', 'c1':'c'}

data = data.apply(lambda x: [d.get(y,y) for y in x])
#alternative solution
#data = data.map(lambda x: [d.get(y,y) for y in x])
print (data)
0    [a, b, c]
1    [b, a, c]
2    [c, a, b]
dtype: object

Or:

data = pd.Series([[d.get(y,y) for y in x] for x in data], index=data.index)

If performance not important:

data = data.explode().replace(d).groupby(level=0).agg(list)
print (data)
0    [a, b, c]
1    [b, a, c]
2    [c, a, b]
dtype: object
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading