I have a df that looks like this:
measurement_3 329
measurement_4 409
measurement_5 508
measurement_6 624
measurement_7 720
measurement_8 846
measurement_9 904
measurement_10 1067
measurement_11 1136
measurement_12 1240
measurement_13 1303
measurement_14 1440
measurement_15 1542
measurement_16 1678
measurement_17 1740
How do I iterate through the column names so that I can get the mean of each column without manually doing it?
I have done the following, but would like a more optimised solution using a for loop:
# Mean
dataset['loading'].fillna(dataset['loading'].mean(), inplace=True)
# Mean std
dataset['measurement_3'].fillna(dataset['measurement_3'].mean() dataset['measurement_3'].std(), inplace=True)
dataset['measurement_4'].fillna(dataset['measurement_4'].mean() dataset['measurement_4'].std(), inplace=True)
dataset['measurement_5'].fillna(dataset['measurement_5'].mean() dataset['measurement_5'].std(), inplace=True)
# continues to measurement_17
CodePudding user response:
Please say no to looping...
measures = df.filter(like='measurement')
df[measures.columns] = measures.fillna(measures.mean() measures.std())
CodePudding user response:
for i in range(3,18):
dataset[f'measurement_{i}'].fillna(dataset[f'measurement_{i}'].mean() dataset[f'measurement_{i}'].std(), inplace=True)
use for loop with f-string
format to access data frame column using name