Python groupby选项卡有问题吗?
我的数据集示例如下所示:Python groupby选项卡有问题吗?,python,pandas,numpy,Python,Pandas,Numpy,我的数据集示例如下所示: data=pd.DataFrame({"Sex":["male", "male", "male", "female", "female"], "Housing":["rent", "rent", "own", "own", "rent"], "Saving accounts":[1000, 1200, 3000, 4000, 5600], "Checking a
data=pd.DataFrame({"Sex":["male", "male", "male", "female", "female"],
"Housing":["rent", "rent", "own", "own", "rent"],
"Saving accounts":[1000, 1200, 3000, 4000, 5600],
"Checking account":[1200, 1540, 6780, 2000, 3000]})
我有如下代码:
data.groupby(['Sex', 'Housing']).agg({'Saving accounts':[min, max, np.mean, np.median, sum],
'Checking account':[min, max, np.mean, np.median, sum]})
我想得到一个表格,它将显示我的基本统计数据,如:基于性别和住房的最小值、最大值等。例如:拥有住房的男性,最小储蓄账户为1000和最大值6000,最小支票账户为2000和最大值5000,关于平均值和中位数也是如此。
我想要这样的东西:(当然是关于其他数据的例子)
然而,我有一个错误:
TypeError: '<=' not supported between instances of 'str' and 'float'
TypeError:“问题是列不是数字
因此,请尝试强制转换为整数或浮点数:
cols = ['Saving accounts', 'Checking account']
data[cols] = data[cols].astype(int)
#data[cols] = data[cols].astype(float)
如果由于某些非数字值与errors='concurve'
一起用于将不可解析的值转换为缺少的值而失败:
cols = ['Saving accounts', 'Checking account']
data[cols] = data[cols].apply(pd.to_numeric, errors='coerce')