I coded a little loop that reduces a dataset by computing the mean of X rows every X rows.
I'm working with a dataset of a certain size (8 Millions rows for 10 columns) and my code takes too long to compute the whole dataset.
I am using Pandas.
Is there a way to improve my loop to make it more efficient and faster ?
# data is the dataset, pd.DataFrame()
#
# nb_row is the number of rows to collapse
# for example : nb_row = 12 would reduce the dataset by x12
# by computing the mean of 12 rows every 12 rows
nb_row = 12
raw_columns = ["f1", "f2", "f3", "f4"] # column names
for i in range(0, len(data), nb_row):
new_df = new_df.append(
pd.DataFrame(
[[data[elem].iloc[i : i + nb_row].mean() for elem in raw_columns]],
columns=raw_columns,
)
)
Thanks