我有这段代码,它运行顺利:
import seaborn as sns
iris = sns.load_dataset('iris')
iris.apply(lambda col: max(col), axis=0)
iris.apply(lambda col: min(col), axis=0)
但是当我尝试获取最大值和最小值之间的差异时,出现错误: TypeError: ("unsupported operand type(s) for -: 'str' and 'str'", 'occurred at index species')
iris.apply(lambda col: max(col) - min(col), axis=0)
我认为既max()
和min()
成功,max() - min()
就会成功了。它有什么问题?
使用 过滤掉字符串列select_dtypes
,因为您不能对字符串进行算术运算(...对吗?!)。然后pd.DataFrame.max/min
,您可以使用 对其进行矢量化,而无需apply
.
v = iris.select_dtypes(exclude=[object])
v.max(0) - v.min(0)
sepal_length 3.6
sepal_width 2.4
petal_length 5.9
petal_width 2.4
dtype: float64
好吧,如果你知道你的数据集,那么 iris 数据集中唯一的非数字列是标签(又名,species
列),所以你可以,
v = iris.drop('species', 1)
其余的都是一样的。呼叫pd.DataFrame.drop
并不会影响原来的数据帧。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句