Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 930
alexzaizar09 Avatar asked Aug 02 '17 03:08

alexzaizar09


People also ask

How do you round a mean value in Python?

Python round() Function The round() function returns a floating point number that is a rounded version of the specified number, with the specified number of decimals. The default number of decimals is 0, meaning that the function will return the nearest integer.

What does AGG function do in pandas?

Pandas DataFrame agg() Method The agg() method allows you to apply a function or a list of function names to be executed along one of the axis of the DataFrame, default 0, which is the index (row) axis. Note: the agg() method is an alias of the aggregate() method.


2 Answers

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
like image 191
Alexander Avatar answered Oct 08 '22 04:10

Alexander


You can use python's round function to get mean value in nearest integer, for example see below mean of LotArea was rounded to nearest int. avg_lot_size = round(home_data['LotArea'].mean())

if home_data['LotArea'].mean() gives value 100056.89 then avg_lot_size would be= 100057

like image 20
Jinesh Bhandari Avatar answered Oct 08 '22 03:10

Jinesh Bhandari