Python 获取Pandas Dataframe的特定列值列表
Pandas是一个用于探索和清理杂乱数据集的Python库,使数据适合提取必要和有价值的见解。Pandas中的Dataframe是一个二维数据结构,非常类似于电子表格、SQL表和Excel数据表。
我们可以使用各种方法来提取特定的列值。
- 使用’.values.tolist()’方法
-
使用’.loc[]’方法
-
使用’.iloc[]’方法
-
使用’get()’函数
方法一:使用’.values.tolist()’方法
‘.values’用于将Python字典的所有相关值提取为普通列表或数组。
‘.tolist()’用于将普通列表或NumPy数组转换为Python列表。
语法
col_vals=df['col_name'].values.tolist()
示例
创建一个包含学生姓名、年龄和最喜欢的科目的表格,并使用tolist()方法提取“最喜欢的科目”列的值。
步骤
- 首先导入所需的库。
-
根据要求创建一个表格。
-
将表格转换为DataFrame对象,并将其命名为df。
-
对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()方法提取“最喜欢科目”列的值。
步骤
- 首先导入所需的库。
-
根据要求创建一个表格。
-
现在将表格转换为DataFrame对象,将其赋值给变量df。
-
对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']
方法3:使用’.iloc []’方法
‘.iloc []’属性根据传递给它的索引值,返回DataFrame的指定列数据或行数据。
语法
col_vals=df.iloc[:,'col_index'].tolist()
示例
创建一个包含学生姓名、年龄和最喜欢的科目的表格,并使用iloc()方法提取“最喜欢的科目”列的值。
步骤
- 首先导入所需的库。
-
根据要求创建一个表格。
-
将表格转换为DataFrame对象时,将其赋值给变量df。
-
将属性’iloc’应用于DataFrame df。
-
使用’.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()函数提取“最喜欢科目”列的值。
步骤
- 首先导入所需库。
-
根据要求创建表格。
-
现在将表格转换为DataFrame对象,变量为df。
-
将函数’get()’应用于DataFrame df。
-
使用’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()’方法。上文讨论的方法简单易懂。