Skip to main content
 首页 » 编程设计

python之无法在 pandas 中执行具有灵活类型错误的 reduce

2025年01月19日9soundcode

我有一个包含 4 个属性的数据集,例如:

  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 

每个属性的数据类型如下:

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

我想计算每个属性的均值和标准差 (std)。

意味着我已经尝试过这段代码:

np.mean('longitude') 

但它给了我这样的错误:

TypeError: cannot perform reduce with flexible type 

请您参考如下方法:

你可以使用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