获取 Pandas DataFrame 中特定列的值列表


Pandas 是一个 Python 库,用于探索和清理杂乱的数据集,并使数据适合于提取必要的和有价值的见解。Pandas 中的 DataFrame 是一种二维数据结构,非常类似于电子表格、SQL 表格和 Excel 数据表。

我们可以使用多种方法来提取特定的列值。

  • 使用 '.values.tolist()' 方法

  • 使用 '.loc[]' 方法

  • 使用 '.iloc[]' 方法

  • 使用 'get()' 函数

方法 1:使用 .values.tolist() 方法

'.values' 用于从 Python 字典中提取与某个键相关联的所有值,作为普通列表或数组。

'.tolist()' 用于将此类普通列表或 NumPy 数组转换为“Python 列表”。

语法

col_vals=df['col_name'].values.tolist()

示例

创建一个包含学生姓名、年龄和最喜欢的学科的表格,并使用 tolist() 方法提取“最喜欢的学科”列的值。

算法

  • 首先导入所需的库。

  • 根据要求创建一个表格。

  • 现在将表格作为变量 df 转换为 DataFrame 对象。

  • 对 DataFrame df 应用属性 '.values'。

  • 获得的输出将是一个 NumPy 数组,现在要将其转换为列表,应用 '.tolist()' 方法,因为我们的需求是列表。

  • 最后,使用内置的 'print()' 函数打印输出。

import pandas as pd import numpy as np #creating a table student_data={ 'Name of the Student': ['Alice', 'Cassie', 'Henry', 'Steven'], 'Age':[15,13,16,14], 'Favourite Subject':['Math', 'Social', 'Science', 'English'] } #Now, we will turn the student_data table into Dataframe. print("DataFrame that we created:") df=pd.DataFrame(student_data) print(df) #tolist() is used to convert the column values into a list. print("Extracted values of desired Column:") col_vals=df['Favourite Subject'].values.tolist() print(col_vals)

输出

DataFrame that we created:
  Name of the Student  Age Favourite Subject
0               Alice   15              Math
1              Cassie   13            Social
2               Henry   16           Science
3              Steven   14           English
Extracted values of desired Column:
['Math', 'Social', 'Science', 'English']

方法 2:使用 '.loc[]' 方法

'.loc[]' 属性返回 DataFrame 的指定列数据。

语法

col_vals=df.loc[:,'col_name'].tolist()

示例

创建一个包含学生姓名、年龄和最喜欢的学科的表格,并使用 loc() 方法提取“最喜欢的学科”列的值。

算法

  • 首先导入所需的库。

  • 根据要求创建一个表格。

  • 现在将表格作为变量 df 转换为 DataFrame 对象。

  • 对 DataFrame df 应用属性 'loc'。

  • 使用 '.tolist()' 方法将数据转换为 Python 列表,因为我们的需求是列表。

  • 最后,使用内置的 'print()' 函数打印输出。

import pandas as pd import numpy as np #creating a table student_data={ 'Name of the Student': ['Alice', 'Cassie', 'Henry', 'Steven'], 'Age':[15,13,16,14], 'Favourite Subject':['Math', 'Social', 'Science', 'English'] } #Now, we will turn the student_data table into Dataframe. print("DataFrame that we created:") df=pd.DataFrame(student_data) print(df) #tolist() is used to convert the column values into a list. print("Extracted values of desired Column:") col_vals=df.loc[:,'Favourite Subject'].tolist() print(col_vals)

输出

DataFrame that we created:
  Name of the Student  Age Favourite Subject
0               Alice   15              Math
1              Cassie   13            Social
2               Henry   16           Science
3              Steven   14           English
Extracted values of desired Column:
['Math', 'Social', 'Science', 'English']

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

方法 3:使用 ‘.iloc[]’ 方法

‘.iloc[]’ 属性返回 DataFrame 的指定列数据或行数据,具体取决于作为参数传递给它的索引值。

语法

col_vals=df.iloc[:,'col_index'].tolist()

示例

创建一个包含学生姓名、年龄和最喜欢的学科的表格,并使用 iloc() 方法提取“最喜欢的学科”列的值。

算法

  • 首先导入所需的库。

  • 根据要求创建一个表格。

  • 现在将表格作为变量 df 转换为 DataFrame 对象。

  • 对 DataFrame df 应用属性 ‘iloc’。

  • 使用 ‘.tolist()’ 方法将数据转换为 Python 列表,因为我们的需求是列表。

  • 最后,使用内置的 ‘print()’ 函数打印输出。

import pandas as pd import numpy as np #creating a table student_data={ 'Name of the Student': ['Alice', 'Cassie', 'Henry', 'Steven'], 'Age':[15,13,16,14], 'Favourite Subject':['Math', 'Social', 'Science', 'English'] } #Now, we will turn the student_data table into Dataframe. print("DataFrame that we created:") df=pd.DataFrame(student_data) print(df) print("Extracted values of desired Column:") col_vals=df.iloc[:,2].tolist() print(col_vals)

输出

DataFrame that we created:
  Name of the Student  Age Favourite Subject
0               Alice   15              Math
1              Cassie   13            Social
2               Henry   16           Science
3              Steven   14           English
Extracted values of desired Column:
['Math', 'Social', 'Science', 'English']

方法 4:使用 get() 函数

‘get()’ 函数从 DataFrame 返回列的值,或从字典返回键的值。

语法

col_vals=df.get('col_name').tolist()

示例

创建一个包含学生姓名、年龄和最喜欢的学科的表格,并使用 get() 函数提取“最喜欢的学科”列的值。

算法

  • 首先导入所需的库。

  • 根据要求创建一个表格。

  • 现在将表格作为变量 df 转换为 DataFrame 对象。

  • 对 DataFrame df 应用函数 ‘get()’。

  • 使用 ‘.tolist()’ 方法将数据转换为 Python 列表,因为我们的需求是列表。

  • 最后,使用内置的 ‘print()’ 函数打印输出。

import pandas as pd import numpy as np #creating a table student_data={ 'Name of the Student': ['Alice', 'Cassie', 'Henry', 'Steven'], 'Age':[15,13,16,14], 'Favourite Subject':['Math', 'Social', 'Science', 'English'] } #Now, we will turn the student_data table into Dataframe. print("DataFrame that we created:") df=pd.DataFrame(student_data) print(df) #tolist() is used to convert the column values into a list. print("Extracted values of desired Column:") column_vals=df.get('Favourite Subject').tolist() print(column_vals)

输出

我们创建的 DataFrame

DataFrame that we created:
  Name of the Student  Age Favourite Subject
0               Alice   15              Math
1              Cassie   13            Social
2               Henry   16           Science
3              Steven   14           English
Extracted values of desired Column:
['Math', 'Social', 'Science', 'English']

结论

这些是从表格中提取特定列值列表的一些方法。但是,我们仍然可以创建更多方法来列出列值。例如,我们可以使用 for 循环遍历列并打印列值列表。我们还可以使用 'apply()' 方法、'numpy.ravel()' 函数,甚至 'iteritems()' 方法。文章中讨论的方法简单易懂。

更新于: 2023年8月10日

5K+ 次浏览

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告