在 Pandas 中,
column
函数用于从 DataFrame 中选择特定列。该函数返回一个 Series 对象,其中包含指定列中的数据。
column(label)其中:
label
:要选择的列的标签(名称)。
参数
column
函数没有其他参数。
返回值
column
函数返回一个 Series 对象,其中包含指定列中的数据。
示例
以下示例展示了如何使用
column
函数:
Python
import pandas as pd创建一个 DataFrame
df = pd.DataFrame({"Name": ["John", "Mary", "Bob"],"Age": [20, 25, 30],"Salary": [1000, 20```python
df.iloc[:, [0, 2]]其中:表示选择所有行。
[0, 2]
表示选择列索引为 0 和 2 的列。
使用 loc 索引器
loc
索引器使用标签索引来选择行和列。要选择特定列的子集,可以使用以下语法:
python
df.loc[:, ["Name", "Salary"]]其中:表示选择所有行。
["Name", "Salary"]
表示选择标签为 "Name" 和 "Salary" 的列。
使用正则表达式
可以使用正则表达式来选择多个列:
python使用正则表达式选择以 "S" 开头的列
s_starting_columns = df.filter(regex="^S")打印以 "S" 开头的列
print(s_starting_columns)输出:Salary
0 1000
1 2000
2 3000
结论
column
函数是选择 DataFrame 中特定列的强大工具。它提供了多种选项来选择列,包括根据标签、索引或正则表达式。
本文地址:https://www.qianwe.com/article/8f8caae40433c4764778.html