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

Question:

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())
Asked By: Anya Pilipentseva

||

Answers:

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())
Answered By: Anoushiravan R

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.

Answered By: Syed Muhammad Zain

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())
Answered By: Timeless
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.