机器学习与深度学习day7——复习日(查看函数参数)
·
前言:花时间复习一下前面的数据处理,一定要看一眼;
函数参数的查看方法
本教程介绍在 VSCode 中查看 Python 函数参数的多种方法。
1. 鼠标悬停查看(Hover)
1.1 操作步骤
- 将鼠标移动到函数名上(如
fillna) - 等待约 0.5 秒,会自动弹出提示框
- 提示框会显示:
- 函数签名(包含所有参数)
- 参数类型提示
- 简短的功能说明
- 参数默认值
1.2 示例演示
import pandas as pd
# 示例数据
data = pd.DataFrame({'A': [1, None, 3], 'B': [4, 5, None]})
# 将鼠标悬停在 fillna 上,会看到完整的参数列表
data.fillna(value=0)
| A | B | |
|---|---|---|
| 0 | 1.0 | 4.0 |
| 1 | 0.0 | 5.0 |
| 2 | 3.0 | 0.0 |
2. 参数提示(Parameter Hints)
2.1 触发方式
- 输入函数名和左括号
(后自动显示
2.2 特点
- 会高亮当前正在输入的参数
- 显示参数类型和默认值
- 可以看到参数的顺序
# 当你输入左括号时,会自动显示参数提示
data.fillna()
3. 查看完整文档
3.1 使用 help() 函数
# 使用 help() 函数查看完整文档
help(data.fillna)
# 输出包含:
# - 函数签名
# - 参数说明(Parameters)
# - 返回值说明(Returns)
# - 使用示例(Examples)
Help on method fillna in module pandas.core.generic:
fillna(
value: 'Hashable | Mapping | Series | DataFrame | None' = None,
*,
method: 'FillnaOptions | None' = None,
axis: 'Axis | None' = None,
inplace: 'bool_t' = False,
limit: 'int | None' = None,
downcast: 'dict | None | lib.NoDefault' = <no_default>
) -> 'Self | None' method of pandas.core.frame.DataFrame instance
Fill NA/NaN values using the specified method.
Parameters
----------
value : scalar, dict, Series, or DataFrame
Value to use to fill holes (e.g. 0), alternately a
dict/Series/DataFrame of values specifying which value to use for
each index (for a Series) or column (for a DataFrame). Values not
in the dict/Series/DataFrame will not be filled. This value cannot
be a list.
method : {'backfill', 'bfill', 'ffill', None}, default None
Method to use for filling holes in reindexed Series:
* ffill: propagate last valid observation forward to next valid.
* backfill / bfill: use next valid observation to fill gap.
.. deprecated:: 2.1.0
Use ffill or bfill instead.
axis : {0 or 'index'} for Series, {0 or 'index', 1 or 'columns'} for DataFrame
Axis along which to fill missing values. For `Series`
this parameter is unused and defaults to 0.
inplace : bool, default False
If True, fill in-place. Note: this will modify any
other views on this object (e.g., a no-copy slice for a column in a
DataFrame).
limit : int, default None
If method is specified, this is the maximum number of consecutive
NaN values to forward/backward fill. In other words, if there is
a gap with more than this number of consecutive NaNs, it will only
be partially filled. If method is not specified, this is the
maximum number of entries along the entire axis where NaNs will be
filled. Must be greater than 0 if not None.
downcast : dict, default is None
A dict of item->dtype of what to downcast if possible,
or the string 'infer' which will try to downcast to an appropriate
equal type (e.g. float64 to int64 if possible).
.. deprecated:: 2.2.0
Returns
-------
Series/DataFrame or None
Object with missing values filled or None if ``inplace=True``.
See Also
--------
ffill : Fill values by propagating the last valid observation to next valid.
bfill : Fill values by using the next valid observation to fill the gap.
interpolate : Fill NaN values using interpolation.
reindex : Conform object to new index.
asfreq : Convert TimeSeries to specified frequency.
Examples
--------
>>> df = pd.DataFrame([[np.nan, 2, np.nan, 0],
... [3, 4, np.nan, 1],
... [np.nan, np.nan, np.nan, np.nan],
... [np.nan, 3, np.nan, 4]],
... columns=list("ABCD"))
>>> df
A B C D
0 NaN 2.0 NaN 0.0
1 3.0 4.0 NaN 1.0
2 NaN NaN NaN NaN
3 NaN 3.0 NaN 4.0
Replace all NaN elements with 0s.
>>> df.fillna(0)
A B C D
0 0.0 2.0 0.0 0.0
1 3.0 4.0 0.0 1.0
2 0.0 0.0 0.0 0.0
3 0.0 3.0 0.0 4.0
Replace all NaN elements in column 'A', 'B', 'C', and 'D', with 0, 1,
2, and 3 respectively.
>>> values = {"A": 0, "B": 1, "C": 2, "D": 3}
>>> df.fillna(value=values)
A B C D
0 0.0 2.0 2.0 0.0
1 3.0 4.0 2.0 1.0
2 0.0 1.0 2.0 3.0
3 0.0 3.0 2.0 4.0
Only replace the first NaN element.
>>> df.fillna(value=values, limit=1)
A B C D
0 0.0 2.0 2.0 0.0
1 3.0 4.0 NaN 1.0
2 NaN 1.0 NaN 3.0
3 NaN 3.0 NaN 4.0
When filling using a DataFrame, replacement happens along
the same column names and same indices
>>> df2 = pd.DataFrame(np.zeros((4, 4)), columns=list("ABCE"))
>>> df.fillna(df2)
A B C D
0 0.0 2.0 0.0 0.0
1 3.0 4.0 0.0 1.0
2 0.0 0.0 0.0 NaN
3 0.0 3.0 0.0 4.0
Note that column D is not affected since it is not present in df2.
3.2 使用 Jupyter 魔法命令
# 使用 ? 查看文档
?data.fillna
# 使用 ?? 查看源码(如果支持),但是一般用ctrl或者断点调试来看-----有点传统编程的感觉了
# ??data.fillna
[31mSignature:[39m
data.fillna(
value: [33m'Hashable | Mapping | Series | DataFrame | None'[39m = [38;5;28;01mNone[39;00m,
*,
method: [33m'FillnaOptions | None'[39m = [38;5;28;01mNone[39;00m,
axis: [33m'Axis | None'[39m = [38;5;28;01mNone[39;00m,
inplace: [33m'bool_t'[39m = [38;5;28;01mFalse[39;00m,
limit: [33m'int | None'[39m = [38;5;28;01mNone[39;00m,
downcast: [33m'dict | None | lib.NoDefault'[39m = <no_default>,
) -> [33m'Self | None'[39m
[31mDocstring:[39m
Fill NA/NaN values using the specified method.
Parameters
----------
value : scalar, dict, Series, or DataFrame
Value to use to fill holes (e.g. 0), alternately a
dict/Series/DataFrame of values specifying which value to use for
each index (for a Series) or column (for a DataFrame). Values not
in the dict/Series/DataFrame will not be filled. This value cannot
be a list.
method : {'backfill', 'bfill', 'ffill', None}, default None
Method to use for filling holes in reindexed Series:
* ffill: propagate last valid observation forward to next valid.
* backfill / bfill: use next valid observation to fill gap.
.. deprecated:: 2.1.0
Use ffill or bfill instead.
axis : {0 or 'index'} for Series, {0 or 'index', 1 or 'columns'} for DataFrame
Axis along which to fill missing values. For `Series`
this parameter is unused and defaults to 0.
inplace : bool, default False
If True, fill in-place. Note: this will modify any
other views on this object (e.g., a no-copy slice for a column in a
DataFrame).
limit : int, default None
If method is specified, this is the maximum number of consecutive
NaN values to forward/backward fill. In other words, if there is
a gap with more than this number of consecutive NaNs, it will only
be partially filled. If method is not specified, this is the
maximum number of entries along the entire axis where NaNs will be
filled. Must be greater than 0 if not None.
downcast : dict, default is None
A dict of item->dtype of what to downcast if possible,
or the string 'infer' which will try to downcast to an appropriate
equal type (e.g. float64 to int64 if possible).
.. deprecated:: 2.2.0
Returns
-------
Series/DataFrame or None
Object with missing values filled or None if ``inplace=True``.
See Also
--------
ffill : Fill values by propagating the last valid observation to next valid.
bfill : Fill values by using the next valid observation to fill the gap.
interpolate : Fill NaN values using interpolation.
reindex : Conform object to new index.
asfreq : Convert TimeSeries to specified frequency.
Examples
--------
>>> df = pd.DataFrame([[np.nan, 2, np.nan, 0],
... [3, 4, np.nan, 1],
... [np.nan, np.nan, np.nan, np.nan],
... [np.nan, 3, np.nan, 4]],
... columns=list("ABCD"))
>>> df
A B C D
0 NaN 2.0 NaN 0.0
1 3.0 4.0 NaN 1.0
2 NaN NaN NaN NaN
3 NaN 3.0 NaN 4.0
Replace all NaN elements with 0s.
>>> df.fillna(0)
A B C D
0 0.0 2.0 0.0 0.0
1 3.0 4.0 0.0 1.0
2 0.0 0.0 0.0 0.0
3 0.0 3.0 0.0 4.0
Replace all NaN elements in column 'A', 'B', 'C', and 'D', with 0, 1,
2, and 3 respectively.
>>> values = {"A": 0, "B": 1, "C": 2, "D": 3}
>>> df.fillna(value=values)
A B C D
0 0.0 2.0 2.0 0.0
1 3.0 4.0 2.0 1.0
2 0.0 1.0 2.0 3.0
3 0.0 3.0 2.0 4.0
Only replace the first NaN element.
>>> df.fillna(value=values, limit=1)
A B C D
0 0.0 2.0 2.0 0.0
1 3.0 4.0 NaN 1.0
2 NaN 1.0 NaN 3.0
3 NaN 3.0 NaN 4.0
When filling using a DataFrame, replacement happens along
the same column names and same indices
>>> df2 = pd.DataFrame(np.zeros((4, 4)), columns=list("ABCE"))
>>> df.fillna(df2)
A B C D
0 0.0 2.0 0.0 0.0
1 3.0 4.0 0.0 1.0
2 0.0 0.0 0.0 NaN
3 0.0 3.0 0.0 4.0
Note that column D is not affected since it is not present in df2.
[31mFile:[39m e:\anaconda\lib\site-packages\pandas\core\generic.py
[31mType:[39m method
3.3 help() 与 ? 的区别
虽然两者都用于查看文档,但它们的输出格式和内容有所不同:
help() 函数
- 来源: Python 内置函数
- 格式: 纯文本格式,在终端中显示
- 内容: 读取对象的
__doc__属性(docstring) - 显示: 使用分页器显示(如
less),可以上下滚动 - 退出: 按
q退出查看 - 适用: 任何 Python 环境(包括普通 Python 解释器)
? 魔法命令
- 来源: IPython/Jupyter 特有功能
- 格式: 带格式的文本,可能包含颜色高亮
- 内容: 除了 docstring,还包括:
- 对象类型信息
- 定义所在文件
- 源代码位置
- 更丰富的格式化
- 显示: 在 Jupyter 中以弹出窗口或下方面板显示
- 适用: 仅在 IPython/Jupyter 环境中可用
方法总结对比
| 方法 | 快捷键 | 优点 | 适用场景 |
|---|---|---|---|
| 鼠标悬停 | 无 | 自动、快速 | 快速查看参数列表 |
| 强制悬停 | Ctrl+K Ctrl+I | 主动触发 | 悬停不显示时 |
| 参数提示 | Ctrl+Shift+Space | 高亮当前参数 | 输入参数时 |
| help() | 无 | 最详细、兼容性好 | 需要完整文档时 |
| ? | 无 | Jupyter 友好、格式美观 | Jupyter 环境中 |
| ?? | 无 | 可查看源码 | 深入理解实现 |
| 跳转定义 | F12 | 直接查看源码 | 深入理解实现 |
更多推荐
所有评论(0)