# Averaging the value of a column in a dataframe

**URL:** <https://discuss.python.org/t/averaging-the-value-of-a-column-in-a-dataframe/18187>\
**Category:** Python Help\
**Created:** [August 13, 2022, 6:55am UTC](https://discuss.python.org/t/averaging-the-value-of-a-column-in-a-dataframe/18187 "2022-08-13T06:55:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohamed\_Fergany](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@Mohamed\_Fergany](https://discuss.python.org/u/Mohamed_Fergany)\
**Post date:** [August 13, 2022, 6:55am UTC](https://discuss.python.org/t/averaging-the-value-of-a-column-in-a-dataframe/18187/1 "2022-08-13T06:55:52Z")

</div>

Hi everyone,

I have this dataframe.

```auto
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

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

```

I’ve also tried

```auto
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 😊 😊

---

<div class="post-metadata">

**Author:** ![abessman](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/abessman/32/8367_2.png) [@abessman](https://discuss.python.org/u/abessman)\
**Post date:** [August 13, 2022, 8:57am UTC](https://discuss.python.org/t/averaging-the-value-of-a-column-in-a-dataframe/18187/2 "2022-08-13T08:57:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mohamed\_Fergany](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@Mohamed\_Fergany](https://discuss.python.org/u/Mohamed_Fergany)\
**Post date:** [August 13, 2022, 10:35am UTC](https://discuss.python.org/t/averaging-the-value-of-a-column-in-a-dataframe/18187/3 "2022-08-13T10:35:33Z")

</div>

@abessman  
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.

---

<div class="post-metadata">

**Author:** ![abessman](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/abessman/32/8367_2.png) [@abessman](https://discuss.python.org/u/abessman)\
**Post date:** [August 13, 2022, 11:23am UTC](https://discuss.python.org/t/averaging-the-value-of-a-column-in-a-dataframe/18187/4 "2022-08-13T11:23:48Z")

</div>

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:

```python
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.

---

<div class="post-metadata">

**Author:** ![Mohamed\_Fergany](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@Mohamed\_Fergany](https://discuss.python.org/u/Mohamed_Fergany)\
**Post date:** [August 13, 2022, 12:02pm UTC](https://discuss.python.org/t/averaging-the-value-of-a-column-in-a-dataframe/18187/5 "2022-08-13T12:02:55Z")

</div>

I’ve found the solution

```auto
mean_meter_reading = mean_meter_reading.apply(round)

```

Thanks @abessman 😊 😊
