如何在Pandas数据帧中的多个列上执行(min,max)?

2024-06-07 10:09:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据帧:

    preds        geoLong      geoLat
8      11     -78.949609   39.154228
9      11    -128.489609   38.154228
10     11      -48.48969   37.154228

我正在对列进行分组(preds

^{pr2}$

当我执行以下操作时,从该列得到的结果不正确:

gbr.agg({'geoLong': 'max'})

Tags: 数据maxaggpr2gbrpredsgeolatgeolong
1条回答
网友
1楼 · 发布于 2024-06-07 10:09:04

你似乎需要:

gbr.agg({'geoLong': 'max', 'geoLat':'min'})

但首先检查dtypes以查看列geoLong和{}是否为数字:

^{pr2}$

如果存在objects(显然是strings),则需要解析它:

cols = ['geoLong','geoLat']
df[cols] = df[cols].astype(float)

如果由于错误数据而返回错误,请使用^{}将所有错误数据替换为NaNs:

ValueError: could not convert string to float: 'l'

cols = ['geoLong','geoLat']
df[cols] = df[cols].apply(pd.to_numeric, errors='coerce')

相关问题 更多 >

    热门问题