Pandas DataFrame get() 方法

定义和用法

get() 方法从 DataFrame 返回指定的列。

如果仅指定一列,则返回值是 Pandas Series 对象。

若要指定多列,请指定数组内的列。结果将是一个新的 DataFrame 对象。

实例

从 DataFrame 中提取 "firstname" 列:

import pandas as pd

data = {
  "firstname": ["Sally", "Mary", "John"],
  "age": [50, 40, 30],
  "qualified": [True, False, False]
}

df = pd.DataFrame(data)
print(df.get("firstname"))

亲自试一试

语法

dataframe.get(key)

参数

参数 描述
key 可选。表示要返回的列的字符串或对象。

返回值

如果只返回一列:Pandas Series 对象。

如果返回多于一列:Pandas DataFrame 对象。