+4 votes
in Programming Languages by (5.4k points)
I want to find the mean and median value of each column in an R dataframe. Instead of looping over columns, is there any pandas-type function to get the summary of the data?

1 Answer

0 votes
by (33.7k points)

The summary() function summarizes the values in each column of the dataframe. It returns min, max, mean, median by column. You can use it.

Here is an example:

> x=c(1,2,3,4,5,6,7)
> y=c(11,12,13,14,15,16,17)
> df = data.frame(x=x, y=y)
> df
  x  y
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 6 16
7 7 17
> summary(df)
       x             y       
 Min.   :1.0   Min.   :11.0  
 1st Qu.:2.5   1st Qu.:12.5  
 Median :4.0   Median :14.0  
 Mean   :4.0   Mean   :14.0  
 3rd Qu.:5.5   3rd Qu.:15.5  
 Max.   :7.0   Max.   :17.0 

...