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

Run same operation on multiple columns with assign?

I am looking for a short way to run the same operation on multiple columns – typically by running it through .assign.

import pandas as pd

df = pd.DataFrame({'a':[' abc ', 'efg', ' hij'], 'b': [' kkk ', 'eee', 'uuu ']})

In this example I have two columns with strings, some of which have leading and trailing spaces. If I want to remove them I would do something like this:

df.assign(a=lambda x: x["a"].str.strip(), b=lambda x: x["b"].str.strip())

basically repeating the same lambda expression for every column. Is there a more convenient way? I do not want to run a loop, because that cannot be method-chained.

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

My first idea was something like this, following the "don’t repeat yourself" principle:

df.assign({col : lambda x: x[col].str.strip() for col in ['a', 'b']})

which of course does not work.

Any suggestions very welcome!

>Solution :

You would need to unpack the dictionary to parameters using **:

df.assign(**{col : lambda x: x[col].str.strip() for col in ['a', 'b']})

output:

     a    b
0  kkk  kkk
1  eee  eee
2  uuu  uuu

ref: PEP 448 for dictionary unpacking

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