Averaging the value of a column in a dataframe

Hi everyone,

I have this dataframe.

mean_meter_reading = pd.DataFrame(
    train.loc[:,['meter', 'meter_reading']].groupby(by = 'meter').mean()

Now I’d like to round these values to only two decimals, so I’ve tried

mean_meter_reading = pd.DataFrame(
    train.loc[:,['meter', 'meter_reading']].groupby(by = 'meter').mean()

I’ve also tried

mean_meter_reading = pd.DataFrame(
    round(train.loc[:,['meter', 'meter_reading']].groupby(by = 'meter').mean(), 2)

but none of them worked. any idea what could be the issue?

Thanks :blush: :blush:

In what way do these attempts not work? What result are you seeing, and what result would you like to have instead?

Perhaps more importantly, why do you want to round these values? Rounding causes loss of data, and is typically only done as a final step when presenting data, not during data processing.


Thanks for your quick reply.
for example if I have 15.123456789, I’d like it to be just 15.1. The reason why I want to round these value is because I wanna put them as labels above the bars in a bar plot.

Still not really clear to me what result you want or what you are starting with.

Is train a DataFrame? I assume so since it has loc.

Why use groupy("meter")? It just sorts the columns by the values in the meter column, but the sorting is lost when you take the mean.

Do you want to calculate the means of the two columns meter and meter_reading and round each value to two decimals? If my assumptions above are correct, that can be done like this:

train.loc[:, ["meter", "meter_reading"]].mean().round(2)

However, this will yield the same result as either of the two attempts you already made, so it seems you want something else. Please clarify.

1 Like

I’ve found the solution

mean_meter_reading = mean_meter_reading.apply(round)

Thanks @abessman :blush: :blush: