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

How to apply an operationg to all column names of a Dataframe except one in Pandas?

I have the following problem. I have a dataframe which has some columns (which title names are strings):

         VALUE_NUMBER    Alias     TS_ns ...  Final_column
0        0.116000        Name_1    3     ...      aaa
1        3.448000        Name_2    34    ...      bbb
2        6.106000        Name_3    7     ...      ccc
3        4.048000        Name_4    54    ...      ddd
4        4.358000        Name_5    32    ...      eee

I have also a function func which performs operations on strings:

def func( string ):
    # do some operations on string...

I would like to apply this function to all the dataframe column titles, except the Alias one.

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

How can I do it? Thanks.

>Solution :

Using apply

Apply a lambda function to all the columns in dataframe using Dataframe.apply(). Inside this lambda function don’t apply function func if column name is ‘Alias’

# axis = 0 applies lambda function to columns
mod_df = df.apply(lambda x: func(x) if x.name != 'Alias' else x, axis=0) 
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