Home > database >  giving an easier name to many columns and applying function to them in pandas
giving an easier name to many columns and applying function to them in pandas

Time:01-29

I have to apply many functions to certain columns in my df. But there are many columns. Is there any way to give these groups of columns a certain name and then apply functions to them?

I am looking for something like

df[['One', 'Two',...'Sixty']] = values
values.apply(lambda x: x.astype(str).str.lower())

CodePudding user response:

Probably you could first create a list of column names and then use pd.DataFrame.isin as follows:

values = ['One', 'Two',...'Sixty']

df.loc[:, df.columns.isin(values)].apply(lambda x: x.astype(str).str.lower())

CodePudding user response:

If performance is not an issue, you can use pandas.DataFrame.applymap :

cols = ['One', 'Two',...'Sixty']

df[cols] = df[cols].astype(str).applymap(lambda x: x.lower())

CodePudding user response:

You can group the columns then use the apply option in pandas:

cols_to_group = ['col1', 'col2']

df.loc[:, cols_to_group] = df.loc[:, cols_to_group].apply(my_function)

my_function can contain the functionality you need to apply to the group.

  • Related