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

Time:07-02

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.

How can I do it? Thanks.

CodePudding user response:

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) 

CodePudding user response:

To select all columns except one column in Pandas DataFrame

df.loc[:, df.columns != <column name>]

  • Related