获取 Pandas DataFrame 的指定列的列表
在本文中,我们将讨论如何获取 Pandas Dataframe 的指定列的列表。首先,我们将 csv 文件读入 pandas 数据帧。
注意:要获取使用的 CSV 文件,请单击此处。
例子:
Python3
# importing pandas module
import pandas as pd
# making data frame from csv
data = pd.read_csv("nba.csv")
# calling head() method
df = data.head(5)
# displaying data
df
Python3
# importing pandas module
import pandas as pd
# making data frame from csv
data = pd.read_csv("nba.csv")
df = data.head(5)
# Converting a specific Dataframe
# column to list using Series.tolist()
Name_list = df["Name"].tolist()
print("Converting name to list:")
# displaying list
Name_list
Python3
# column 'Name' as series object
print(type(df["Name"]))
# Convert series object to a list
print(type(df["Name"].values.tolist()
Python3
# importing pandas module
import pandas as pd
# making data frame from csv
data = pd.read_csv("nba.csv")
df = data.head(5)
# Converting a specific Dataframe column
# to list using numpy.ndarray.tolist()
Name_list = df["Name"].values.tolist()
print("Converting name to list:")
# displaying list
Name_list
Python3
# Select a column from dataframe
# as series and get a numpy array
print(type(df["Name"].values))
# Convert numpy array to a list
print(type(df["Name"].values.tolist()
Python3
# importing pandas module
import pandas as pd
# making data frame from csv
data = pd.read_csv("nba.csv")
df = data.head(5)
# Converting a specific Dataframe
# column to list using list()
# function in Python
Name_List = list(df["Name"])
print("Converting name to list:")
# displaying list
Name_List
Python3
# Converting index column to list
index_list = df.index.tolist()
print("Converting index to list:")
# display index as list
index_list
输出:
让我们看看如何获取 Pandas DataFrame 的指定列的列表:
我们将使用三种不同的方式将“名称”列转换为列表。
1. 使用Series.tolist()
从数据框中,我们使用返回 Series 对象的 []运算符选择列“名称”。接下来,我们将使用 Series 类提供的函数Series.to_list() 来转换系列对象并返回一个列表。
Python3
# importing pandas module
import pandas as pd
# making data frame from csv
data = pd.read_csv("nba.csv")
df = data.head(5)
# Converting a specific Dataframe
# column to list using Series.tolist()
Name_list = df["Name"].tolist()
print("Converting name to list:")
# displaying list
Name_list
输出:
让我们分解一下,看看类型
Python3
# column 'Name' as series object
print(type(df["Name"]))
# Convert series object to a list
print(type(df["Name"].values.tolist()
输出:
2. 使用numpy.ndarray.tolist()
从数据框中,我们使用 []运算符选择“名称”列,该运算符返回一个系列对象并使用 Series.Values 从系列对象中获取一个 NumPy 数组。接下来,我们将使用 NumPy 数组提供的函数tolist() 将其转换为列表。
Python3
# importing pandas module
import pandas as pd
# making data frame from csv
data = pd.read_csv("nba.csv")
df = data.head(5)
# Converting a specific Dataframe column
# to list using numpy.ndarray.tolist()
Name_list = df["Name"].values.tolist()
print("Converting name to list:")
# displaying list
Name_list
输出:
同样,将其分解
Python3
# Select a column from dataframe
# as series and get a numpy array
print(type(df["Name"].values))
# Convert numpy array to a list
print(type(df["Name"].values.tolist()
输出:
3. 使用Python list()函数
您还可以使用Python list()函数和可选的可迭代参数将列转换为列表。
Python3
# importing pandas module
import pandas as pd
# making data frame from csv
data = pd.read_csv("nba.csv")
df = data.head(5)
# Converting a specific Dataframe
# column to list using list()
# function in Python
Name_List = list(df["Name"])
print("Converting name to list:")
# displaying list
Name_List
输出:
将索引列转换为列表
通过调用 pandas.DataFrame.index 将索引列作为数组返回,然后调用 index_column.tolist() 将 index_column 转换为列表,可以将索引列转换为列表。
Python3
# Converting index column to list
index_list = df.index.tolist()
print("Converting index to list:")
# display index as list
index_list
输出: