【数据分析】python-pandas速查文档(3)
python-pandas速查文档(3)博客主页:源码速查本文档共 4 部分,当前为第 3 部分文章目录python-pandas速查文档(3)六、公开方法速查(按功能分类)equals():对象相等判断eval():表达式计算factorize():因子编码floor():时间向下取整floordiv():整除运算flush():刷新到磁盘format():格式化输出fromordinal():序数转时间戳fromtimestamp():秒数转时间戳ge():大于等于比较gt():大于比较holds_integer():是否存整数值identical():严格相同判断indexer_at_time():特定时间定位infer_freq():推断频率infer_objects():推断对象类型intersection():交集is_boolean():布尔类型判断is_categorical():分类类型判断is_dtype():类型匹配判断is_floating():浮点类型判断is_integer():整数类型判断is_numeric():数值类型判断is_object():对象类型判断isetitem():按位置设置值isna():缺失值检测isnull():缺失值检测(同义)isocalendar():ISO日历信息isoweekday():ISO星期序号items():逐列迭代iterrows():逐行迭代itertuples():命名元组迭代keys():HDF5键列表kurt():峰度系数kurtosis():峰度(同义)le():小于等于比较list():列表访问器入口lreshape():宽表转长表lt():小于比较mod():取余运算mode():众数month_name():月份名称mul():乘法multiply():乘法(同义)ne():不等于比较nlargest():最大的N个值notna():非缺失判断notnull():非缺失(同义)now():当前时间戳nsmallest():最小的N个值nunique():唯一值数量parse():解析Excel工作表pop():移除并返回列pow():幂运算qcut():分位数分箱radd():反向加法ravel():展平为一维数组rdiv():反向除法八、快速索引方法快速索引九、参考资料【pandas 公开成员统计】:共 41 个公开类,628 个公开方法,239 个公开属性六、公开方法速查(按功能分类)本部分继续列出公开方法详细条目。equals():对象相等判断Index.equals(self, other: 'object') - 'bool'归属:pandas.Index说明:判断两个 Index 对象是否完全相等(包括值、名称、dtype)。importpandasaspd idx1=pd.Index(["北京","上海","广州"],name="城市")idx2=pd.Index(["北京","上海","广州"],name="城市")idx3=pd.Index(["北京","上海","深圳"],name="城市")print("相同:",idx1.equals(idx2),"不同:",idx1.equals(idx3))# 输出: True Falseeval():表达式计算DataFrame.eval(self, expr: 'str', *, inplace: 'bool' = False, **kwargs) - 'Any | None'归属:pandas.DataFrame说明:在 DataFrame 列上执行字符串表达式计算,比手动写列运算更简洁且支持多核加速。importpandasaspd 商品表=pd.DataFrame({"单价":[100,200,50],"数量":[3,1,10]})商品表.eval("金额 = 单价 * 数量",inplace=True)print(商品表)# 输出:# 单价 数量 金额# 0 100 3 300# 1 200 1 200# 2 50 10 500归属:pandas说明:pandas 顶层 eval,支持对局部或全局变量的表达式求值。importpandasaspd df=pd.DataFrame({"A":[1,2,3],"B":[4,5,6]})result=pd.eval("df.A + df.B")print(result.tolist())# 输出: [5, 7, 9]factorize():因子编码pd.factorize(values, sort: 'bool' = False, use_na_sentinel: 'bool' = True, size_hint: 'int | None' = None) - 'tuple[np.ndarray, np.ndarray | Index]'归属:pandas说明:将字符串/分类值编码为整数标签和唯一值映射,适合替代 sklearn 的 LabelEncoder。importpandasaspd 城市列表=["北京","上海","北京","广州","上海"]编码,唯一值=pd.factorize(城市列表)print("整数编码:",编码)# 输出: [0, 1, 0, 2, 1]print("唯一值:",唯一值.tolist())# 输出: ['北京', '上海', '广州']floor():时间向下取整DatetimeIndex.floor(self, *args, **kwargs)归属:pandas.DatetimeIndex说明:将 DatetimeIndex 中的时间向下对齐到指定频率(小时/天/周等)。importpandasaspd 日期=pd.DatetimeIndex(["2026-05-28 15:45:30","2026-05-29 23:10:00"])print("以小时取整:",日期.floor("h").tolist())# 输出: [Timestamp('2026-05-28 15:00'), Timestamp('2026-05-29 23:00')]归属:pandas.Timedelta说明:将 Timedelta 向下取整到指定精度。importpandasaspd td=pd.Timedelta("3 hours 45 minutes")print(td.floor("h"))# 输出: 0 days 03:00:00归属:pandas.Timestamp说明:将时间戳向下取整到指定频率。importpandasaspd ts=pd.Timestamp("2026-05-28 15:45:30")print(ts.floor("h"))# 输出: 2026-05-28 15:00:00floordiv():整除运算DataFrame.floordiv(self, other, axis: 'Axis' = 'columns', level=None, fill_value=None) - 'DataFrame'归属:pandas.DataFrame说明:对 DataFrame 逐元素执行整除//运算。importpandasaspd df=pd.DataFrame({"总量":[100,250,333],"每组人数":[3,5,10]})df["组数"]=df["总量"].floordiv(df["每组人数"])print(df)# 输出:# 总量 每组人数 组数# 0 100 3 33# 1 250 5 50# 2 333 10 33flush():刷新到磁盘HDFStore.flush(self, fsync: 'bool' = False) - 'None'归属:pandas.HDFStore说明:将 HDF5 存储中的缓冲区数据强制写入磁盘,适合断电或异常安全场景。frompathlibimportPathimportpandasaspd 路径=Path("测试.h5")store=pd.HDFStore(路径,mode="w")store["data"]=pd.DataFrame({"x":[1,2]})store.flush(fsync=True)store.close()路径.unlink(missing_ok=True)format():格式化输出Index.format(self, name: 'bool' = False, formatter: 'Callable | None' = None, na_rep: 'str_t' = 'NaN') - 'list[str_t]'归属:pandas.Index说明:将 Index 各元素转为格式化后的字符串列表,支持自定义格式化函数。importpandasaspd 日期=pd.DatetimeIndex(["2026-05-28","2026-05-29"],name="日期")print(日期.format(formatter=lambdax:x.strftime("%Y/%m/%d")))# 输出: ['2026/05/28', '2026/05/29']fromordinal():序数转时间戳Timestamp.fromordinal(cls, ordinal, tz=None)归属:pandas.Timestamp说明:从 Python datetime 的序数(公历日序号)构造 Timestamp。importpandasaspd ts=pd.Timestamp.fromordinal(738613)# 大约对应 2026-05-28print(ts)# 输出: 约 2026-05-28 00:00:00fromtimestamp():秒数转时间戳Timestamp.fromtimestamp(cls, ts, tz=None)归属:pandas.Timestamp说明:从 Unix 时间戳(秒级浮点数)构造 Timestamp。importpandasaspd ts=pd.Timestamp.fromtimestamp(1700000000)print(ts)# 输出: 约 2023-11-15ge():大于等于比较DataFrame.ge(self, other, axis: 'Axis' = 'columns', level=None) - 'DataFrame'归属:pandas.DataFrame说明:逐元素比较是否 = 给定值,返回布尔 DataFrame。importpandasaspd 成绩表=pd.DataFrame({"语文":[85,90,78],"数学":[88,92,80]})及格=成绩表.ge(80)print(及格)# 输出:# 语文 数学# 0 True True# 1 True True# 2 False True归属:pandas.Series说明:逐元素比较是否 = 给定值。importpandasaspd s=pd.Series([120,80,150],name="销售额")print(s.ge(100))# 输出:# 0 True# 1 False# 2 Truegt():大于比较DataFrame.gt(self, other, axis: 'Axis' = 'columns', level=None) - 'DataFrame'归属:pandas.DataFrame说明:逐元素比较是否 给定值。importpandasaspd df=pd.DataFrame({"销量":[120,80,150]})print(df["销量"].gt(100).tolist())# 输出: [True, False, True]holds_integer():是否存整数值Index.holds_integer(self) - 'bool'归属:pandas.Index说明:判断 Index 中存储的元素是否可无损转为整数。importpandasaspd 浮点索引=pd.Index([1.0,2.0,3.0])整数索引=pd.Index([1,2,3])print("浮点存整数:",浮点索引.holds_integer(),"整数存整数:",整数索引.holds_integer())# 输出: True Trueidentical():严格相同判断Index.identical(self, other) - 'bool'归属:pandas.Index说明:判断两个 Index 是否从头到尾完全相同(比 equals 更严格,检查名称和属性)。importpandasaspd idx1=pd.Index([1,2,3