Pandas教学-18:number与numeric的区别

一、词性不同:一个是名词,一个是形容词

词 词性 含义 中文
number 名词 数、数字、数量、号码 数字、号码、数量
numeric 形容词 数值的、数字形式的 数值(型)的
复制代码
a number        一个数字 / 一个号码
number of ...   ......的数量
numeric value   数值
numeric keypad  数字小键盘

关键理解:

  • number 指的是**"那个东西"**(一个数、一个编号)
  • numeric 描述的是**"性质"**(它是不是数值型的)

二、英语中的实际用法差异

number 用于具体事物

复制代码
phone number     电话号码      ← 是 number
student number   学号          ← 是 number
a large number of students   大量学生

注意一个反直觉点 :电话号码是 number,但不是 numeric ------因为电话号码可能是 "010-8888",含连字符,不能参与加减乘除。这就是"名字叫 number,但数值性质上是文本"的典型例子(也正好对应 pandas 里的"学号"列)。

numeric 用于描述性质

复制代码
numeric value       数值(能参与运算)
numeric data        数值型数据
numeric computation 数值计算
对比 例子 能不能算加减
number(号码) "010-8888"、"2024001" ❌ 不能
numeric(数值) 88、92.5 ✅ 能

三、词源与近义词

  • number ← 拉丁语 numerus(数)→ 古法语 nombre
  • numeric / numerical ← 拉丁语 numericus,是 number 的形容词形式

近义词辨析:

词 含义 例子
number 数、号码(名词) 3, 100, phone number
numeric 数值的(形容词,偏技术/计算) numeric type, numeric keypad
numerical 数值的(形容词,偏数学/学术) numerical analysis(数值分析)
digit 单个数字字符(0--9) "2024" has 4 digits
numeral 数字符号/记数系统 Roman numerals(罗马数字)

记忆 :numeric 更"工程师味"(编程里常见),numerical 更"学术味"(数学论文里常见),日常编程用 numeric 就够了。


四、在 pandas 里的具体差别(最实用部分)

你在写 pandas 代码时会遇到这两个词,但它们在 pandas 里出现在不同位置、不能互换:

场景 正确写法 错误写法 实测结果
聚合时跳过文本列 df.mean(numeric_only=True) --- ✅ 正常
按类型选列 df.select_dtypes(include='number') include='numeric' ❌ TypeError: data type 'numeric' not understood
判断某列是否数值型 pd.api.types.is_numeric_dtype(s) --- ✅ 正常

实测证据:

python 复制代码
df = pd.DataFrame({
    '姓名': ['张三', '李四'],
    '年龄': [20, 21],
    '成绩': [88.5, 92.0],
    '是否及格': [True, False],
    '学号': ['2024001', '2024002'],
})

df.select_dtypes(include='number').columns.tolist()
# ['年龄', '成绩']                        ✅ 用 number

df.select_dtypes(include='numeric')
# TypeError: data type 'numeric' not understood     ❌
df.select_dtypes(include='numbers')
# TypeError: data type 'numbers' not understood     ❌ 复数也不行

为什么会这样?(历史原因,记住即可)

  • numeric_only 是参数名 (布尔开关),英文里习惯用形容词:numeric_only=True("只限数值的")
  • 'number' 是 dtype 家族名(数据类型别名),pandas/numpy 里把它当作一个类型类目在用
  • 这是 pandas 历史演进留下的命名不一致 ,没有语法规律可循,记住这两处固定写法就行

顺带说明:numeric_only=True 的一个坑

python 复制代码
df.mean(numeric_only=True)
复制代码
年龄      20.50
成绩      90.25
是否及格     0.50      ← bool 列也被算进去了!

bool 在 pandas 里被当作数值 (True=1、False=0),所以"是否及格"也被求了均值 0.5。如果你不想要它,要显式排除:

python 复制代码
df.select_dtypes(include='number').drop(columns=['是否及格']).mean()

补充:pd.api.types.is_numeric_dtype() 对 bool 也返回 True,对文本返回 False,实测:

python 复制代码
姓名     str      False
年龄     int64    True
成绩     float64  True
是否及格  bool     True      ← 注意
学号     str      False

补充:np.number 是 numpy 的抽象基类

python 复制代码
np.number                                      # <class 'numpy.number'>
isinstance(np.int64(1), np.number)             # True
isinstance(1.5, np.number)                     # False  ← Python 原生 float 不是
isinstance(True, np.number)                    # False  ← numpy 里 bool 不算 number
isinstance('1', np.number)                     # False

有意思的差异:pandas 认为 bool 是数值,numpy 认为不是。所以:

判断方式 bool 算数值吗
pd.api.types.is_numeric_dtype(bool列) ✅ 算(True)
isinstance(True, np.number) ❌ 不算(False)

这个差异在跨库写代码时要留意。


五、对照总结表

number numeric
词性 名词 形容词
中文 数字、号码、数量 数值的
指什么 具体的一个数/编号 数据类型的一种性质
pandas 中的位置 select_dtypes(include='number') mean(numeric_only=True)
能否互换 绝对不能 绝对不能
反直觉例子 电话号码是 number,但非 numeric 布尔值在 pandas 里算 numeric

六、一句话记住

number 是"东西",numeric 是"性质"。

  • 说"这个名字是个号码" → number(phone number、学号)
  • 说"这列能做加减法" → numeric(numeric value、numeric_only)

pandas 里只需死记两条:

选列用 include='number',聚合用 numeric_only=True。

相关推荐
梦想不只是梦与想4 小时前
Python 中subprocess 模块:执行外部命令
python·subprocess·调用外部命令
懂点Tech4 小时前
ctfshow MISC入门图片篇(信息附加):misc41解题思路
开发语言·python
爱上纯净的蓝天9 小时前
不用云做 RAG:一个本地笔记问答工具的取舍与坑
python·开源·大模型·rag·本地部署
cspttty9 小时前
2027风险管理岗准备路线:数学与应用数学如何补齐金融、SQL与Python
开发语言·python·金融
用户8356290780519 小时前
如何使用 Python 格式化 Excel 单元格中的长文本
后端·python
我命由我123459 小时前
Python 开发 - Python 的 pathlib
java·开发语言·后端·python·学习·java-ee·学习方法
严谨的麻辣烫10 小时前
Python HTTP 连接池怎么用?从连接复用到请求异常排查
开发语言·python·http·requests·网络排查
摇滚侠10 小时前
《Java并发编程之美》第 4 章 Java 并发包中原子操作类原理剖析 阅读笔记 7
java·笔记·python
用户83562907805110 小时前
如何使用 Python 在 Word 文档中创建和格式化列表
后端·python