Python – Unable to perform reduce with flexible type errors in pandas

Unable to perform reduce with flexible type errors in pandas… here is a solution to the problem.

Unable to perform reduce with flexible type errors in pandas

I have a dataset with 4 properties, for example:

  taxi id   date time   longitude   latitude
0   1   2/2/2008 15:36  116.51  39.92
1   1   2/2/2008 15:46  116.51  39.93
2   1   2/2/2008 15:56  116.51  39.91
3   1   2/2/2008 16:06  116.47  39.91
4   1   2/2/2008 16:16  116.47  39.92

The data type for each property is as follows:

taxi id    dtype('int64')
date time  dtype('O')
longitude   dtype('float64')
latitude    dtype('float64')

I want to calculate the mean and standard deviation (STD) for each attribute.

Means I’ve tried this code:

np.mean('longitude')

But it gives me errors like this :

TypeError: cannot perform reduce with flexible type

Solution

You can use pandas describe

df.describe()
Out[878]: 
           taxi   id   longitude   latitude
count  5.000000  5.0    5.000000   5.000000
mean   2.000000  1.0  116.494000  39.918000
std    1.581139  0.0    0.021909   0.008367
min    0.000000  1.0  116.470000  39.910000
25%    1.000000  1.0  116.470000  39.910000
50%    2.000000  1.0  116.510000  39.920000
75%    3.000000  1.0  116.510000  39.920000
max    4.000000  1.0  116.510000  39.930000

Related Problems and Solutions