Reputation: 12509
I have the below code which rounds every column in my dataframe:
def column_round(decimals):
return partial(pd.Series.round, decimals=decimals)
df = df.apply(column_round(2))
I'd like to do this to every other column, in a new dataframe that I have. I believe i read somewhere that ::
is a built in notation for stepping, but i'm not quite sure how to use it.
Upvotes: 1
Views: 882
Reputation: 393963
You can do:
df[df.columns[::2]].apply(column_round(2))
This steps over the df columns so you can sub-select them
Example:
In [2]:
df = pd.DataFrame(columns=list('abcdefgh'))
df
Out[2]:
Empty DataFrame
Columns: [a, b, c, d, e, f, g, h]
Index: []
In [3]:
df.columns[::2]
Out[3]:
Index(['a', 'c', 'e', 'g'], dtype='object')
Upvotes: 2