用法精讲
pandas.api.types.is_number
语法
pandas.api.types.is_number(obj)
Check if the object is a number. Returns True when the object is a number, and False if is not. Parameters:
- obj: any type. The object to check if is a number. Returns:
- bool. Whether obj is a number or not.
参数
- obj (必填):表示需要检查的对象,它可以是任何类型。
功能
检查给定的对象 obj 是否是数字,它包括整数、浮点数以及布尔值 (因为布尔值是整数的子类)。
返回值
返回一个布尔值,如果 obj 是一个数字,则返回 True;反之,则返回 False。
用法
from pandas.api.types import is_number
# 整数
print(is_number(1))
# 浮点数
print(is_number(7.15))
# 布尔值
print(is_number(False))
# 字符串
print(is_number("foo"))
# 字符串形式的数字
print(is_number("5"))
运行结果
True
True
True
False
False
pandas.api.types.is_re
语法
pandas.api.types.is_re(obj)
Check if the object is a regex pattern instance. Parameters:
- obj: The object to check Returns:
- bool. Whether obj is a regex pattern.
参数
- obj (必填):表示需要被检查的对象,它可以是任何类型的对象,但最常见的是字符串,因为正则表达式通常以字符串形式表示。
功能
用于检查给定的对象是否是一个正则表达式模式对象,正则表达式模式对象通常是由 re.compile() 函数或其他正则表达式库生成的模式对象。
返回值
如果 obj 是一个正则表达式模式对象,则返回 True;否则,返回 False。
用法
import pandas as pd
import re
# 创建一个正则表达式模式对象
pattern = re.compile(r'\d+')
# 检查对象是否为正则表达式模式
is_regex = pd.api.types.is_re(pattern)
print(is_regex)
# 检查一个非正则表达式模式对象
non_pattern = "this is not a pattern"
is_regex = pd.api.types.is_re(non_pattern)
print(is_regex)
运行结果
True
False
pandas.api.types.is_re_compilable
语法
pandas.api.types.is_re_compilable(obj)
Check if the object can be compiled into a regex pattern instance. Parameters:
- obj: The object to check Returns:
- bool. Whether obj can be compiled as a regex pattern.
参数
- obj (必填):表示需要被检查的对象,它可以是任何类型的对象。
功能
用于检查给定对象是否可以被用于正则表达式编译。
返回值
返回一个布尔值,如果对象可以被编译为正则表达式,则返回 True,否则返回 False。
用法
from pandas.api.types import is_re_compilable
print(is_re_compilable(".*"))
print(is_re_compilable(1))
运行结果
True
False
pandas.api.types.is_scalar
语法
pandas.api.types.is_scalar(val)
Return True if given object is scalar. Parameters:
- val: object. This includes: numpy array scalar (e.g. np.int64), Python builtin numerics, Python builtin byte arrays and strings, None, datetime.datetime, datetime.timedelta, Period, decimal.Decimal, Interval, DateOffset, Fraction, Number. Returns:
- bool. Return True if given object is scalar.
参数
- val (必填):需要检查的值,可以是任何类型的对象。
功能
检查给定的值是否为标量,即一个单一的值,而不是一个集合或一个数据结构,标量可以是数字 (整数或浮点数)、字符串、布尔值、None 等。
返回值
返回一个布尔值:
True:如果 val 是标量,则返回 True。False:如果 val 不是标量 (例如列表、元组、字典、数据帧等),则返回 False。
用法
import pandas as pd
print(pd.api.types.is_scalar(1))
print(pd.api.types.is_scalar('hello'))
print(pd.api.types.is_scalar([1, 2, 3]))
print(pd.api.types.is_scalar({'a': 1, 'b': 2}))
print(pd.api.types.is_scalar(pd.DataFrame({'A': [1, 2]})))
运行结果
True
True
False
False
False
pandas.Index 类
语法
class pandas.Index(data=None, dtype=None, copy=False, name=None, tupleize_cols=True)
Immutable sequence used for indexing and alignment. The basic object storing axis labels for all pandas objects. Changed in version 2.0.0: Index can hold all numpy numeric dtypes (except float16). Previously only int64/uint64/float64 dtypes were accepted. Parameters:
- data: array-like (1-dimensional). Used to initialize the index.
- dtype: str, numpy.dtype, or ExtensionDtype, optional. Data type for the output Index.
- copy: bool, default False. Copy input data.
- name: object. Name to be stored in the index.
- tupleize_cols: bool (default: True). When True, attempt to create a MultiIndex if possible.
参数
- data (可选,默认值为 None):用于初始化索引的数据,可以是数组、序列、字典、标量值等,如果不提供数据,则默认创建空的索引。
- dtype (可选,默认值为 None):指定索引的数据类型,如果不指定,则根据输入数据自动确定数据类型。
- copy (可选,默认值为 False):布尔值,如果设置为 True,则创建输入数据的副本。
- name (可选,默认值为 None):字符串,用于给索引命名。
- tupleize_cols (可选,默认值为 True):布尔值,如果设置为 True,则将多层次的列名转换为元组。
功能
创建一个 Index 对象,作为 DataFrame 和 Series 的索引。
返回值
返回值是一个 Index 对象,包含了输入数据的各个元素。
用法
import pandas as pd
# 创建一个简单的索引
idx = pd.Index([1, 2, 3, 4], name='numbers')
# 创建一个字符串索引
str_idx = pd.Index(['a', 'b', 'c'], dtype='object', name='letters')
# 使用索引创建 Series
s = pd.Series([10, 20, 30], index=str_idx)
print(idx, '\n')
print(str_idx, '\n')
print(s)
运行结果
Index([1, 2, 3, 4], dtype='int64', name='numbers')
Index(['a', 'b', 'c'], dtype='object', name='letters')
letters
a 10
b 20
c 30
dtype: int64


