In pandas can you aggregate by mean and round that mean to the nearest int?

alexzaizar09 picture alexzaizar09 · Aug 2, 2017 · Viewed 15k times · Source

So I have 169 columns which have been treated to leave 1=for yes and 0= for no, now I need to aggregate the 2 million rows by mean, and the round that results to the nearest int, how could I get that?

The image is just showing that the values per column are either 0 or 1

enter image description here

Answer

Alexander picture Alexander · Aug 2, 2017

If data is your dataframe, you can get the mean of all the columns as integers simply with:

data.mean().astype(int)  # Truncates mean to integer, e.g. 1.95 = 1

or, as of version 0.17.0:

data.mean().round(0)  # Rounds mean to nearest integer, e.g. 1.95 = 2 and 1.05 = 1