You can apply your normalization on all desired columns at once:
sr_df[['340.0', '341.0', '342.0']].apply(lambda x: ((x-x.mean()) / (x.std())))
sr_df[['340.0', '341.0', '342.0']]= sr_df[['340.0', '341.0', '342.0']].apply(lambda x: ((x-x.mean()) / (x.std())))
>>> sr_df
Time 340.0 341.0 342.0 Mode
0 11:30:15 PM -0.259828 0.073922 -0.500626 light
1 11:31:15 PM -0.844441 -1.034910 -0.650814 auto
2 11:32:15 PM 1.104269 0.960988 1.151440 auto
Better yet, you can apply it to all numeric columns (if that's what you're going for):
# Get a list of numeric columns:
cols = list(sr_df.select_dtypes(include=[np.number]).columns.values)
sr_df[cols] = sr_df[cols].apply(lambda x: ((x-x.mean()) / (x.std())))
Fixing your code:
If you want to fix your code, you can apply your function on a column of your dataframe (rather than applying it on the series). The reason it doesn't work on a series is outlined in this answer by @BrenBarn:
When you use apply on a series
, your function is called on each element. When you use apply on a DataFrame
, your function is called on each column.
So the way you're doing it, you're trying to get the mean
and std
of a float, and floats do not have such attributes, leading to your error: AttributeError: 'float' object has no attribute 'mean'
# this works:
sr_df[['340.0']].apply(lambda x: (x - x.mean()) / (x.std()))
# This doesn't:
# sr_df['340.0'].apply(lambda x: (x - x.mean()) / (x.std()))
# The difference is this:
>>> type(sr_df['340.0'])
<class 'pandas.core.series.Series'>
>>> type(sr_df[['340.0']])
<class 'pandas.core.frame.DataFrame'>