I have a DataFrame ave_data
that contains the following:
ave_data
Time F7 F8 F9
00:00:00 43.005593 -56.509746
df.assign
is specifically for this purpose. It returns a copy to avoid changing the original dataframe and/or raising SettingWithCopyWarning
. It works as follows:
data_with_ave = ave_data.assign(average = ave_data.mean(axis=1, numeric_only=True))
This function can also create multiple columns at the same time:
data_with_ave = ave_data.assign(
average = ave_data.mean(axis=1, numeric_only=True),
median = ave_data.median(axis=1, numeric_only=True)
)
As of pandas 0.36, you can even reference a column just created to create another:
data_with_ave = ave_data.assign(
average = ave_data.mean(axis=1, numeric_only=True),
isLarge = lambda df: df['average'] > 10
)