Pandas数据框架是用来处理Python中的表格数据的。在这篇文章中,我们将讨论如何在Python中从一个数据框架中选择一行。我们还将讨论如何使用布尔运算符从pandas数据框中选择数据。
使用iloc属性从数据框架中选择行
iloc 属性包含一个_iLocIndexer 对象,它可以作为一个数据框架中的行的有序集合。iloc 属性的功能类似于 列表索引。你可以使用iloc 属性从数据框架中选择一条记录。为此,你可以简单地使用带有iloc 属性的方括号内的行的位置来选择pandas数据框架的某一行,如下图所示。
myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
position=1
row=myDf.iloc[position]
print("The row at position {} is :{}".format(position,row))
输出
The dataframe is:
Class Roll Name
0 1 11 Aditya
1 1 12 Chris
2 1 13 Sam
3 2 1 Joel
4 2 22 Tom
5 2 44 Samantha
6 3 33 Tina
7 3 34 Amy
The row at position 1 is :Class 1
Roll 12
Name Chris
Name: 1, dtype: object
在这里,你可以看到iloc 属性将指定位置的行作为输出。
在Python中使用loc属性从数据框架中选择行
数据框架的loc 属性的工作方式与Python字典的键类似。 loc 属性包含一个_LocIndexer 对象,你可以用它来从pandas 数据框中选择行。你可以使用方括号内的索引标签与loc 属性来访问pandas系列的元素,如下所示。
myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
index=2
row=myDf.loc[index]
print("The row at index {} is :{}".format(index,row))
输出。
The dataframe is:
Class Roll Name
0 1 11 Aditya
1 1 12 Chris
2 1 13 Sam
3 2 1 Joel
4 2 22 Tom
5 2 44 Samantha
6 3 33 Tina
7 3 34 Amy
The row at index 2 is :Class 1
Roll 13
Name Sam
Name: 2, dtype: object
如果你为数据框架定义了一个自定义的索引,你可以使用某行的索引值来从pandas数据框架中选择该行,如下图所示。
myDf=pd.read_csv("samplefile.csv",index_col=0)
print("The dataframe is:")
print(myDf)
index=1
row=myDf.loc[index]
print("The row at index {} is :{}".format(index,row))
输出结果。
The dataframe is:
Roll Name
Class
1 11 Aditya
1 12 Chris
1 13 Sam
2 1 Joel
2 22 Tom
2 44 Samantha
3 33 Tina
3 34 Amy
The row at index 1 is : Roll Name
Class
1 11 Aditya
1 12 Chris
1 13 Sam
如果你有一个多级索引,你可以使用索引来从数据框架中选择行,如下图所示。
myDf=pd.read_csv("samplefile.csv",index_col=[0,1])
print("The dataframe is:")
print(myDf)
index=(1,12)
row=myDf.loc[index]
print("The row at index {} is :{}".format(index,row))
输出。
The dataframe is:
Name
Class Roll
1 11 Aditya
12 Chris
13 Sam
2 1 Joel
22 Tom
44 Samantha
3 33 Tina
34 Amy
The row at index (1, 12) is :Name Chris
Name: (1, 12), dtype: object
在潘达斯数据框架中使用列名选择列
要从一个数据框架中选择一个列,你可以使用带方括号的列名,如下图所示。
myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
column_name="Class"
column=myDf[column_name]
print("The {} column is :{}".format(column_name,column))
输出。
The dataframe is:
Class Roll Name
0 1 11 Aditya
1 1 12 Chris
2 1 13 Sam
3 2 1 Joel
4 2 22 Tom
5 2 44 Samantha
6 3 33 Tina
7 3 34 Amy
The Class column is :0 1
1 1
2 1
3 2
4 2
5 2
6 3
7 3
Name: Class, dtype: int64
如果你想从一个数据框架中选择一个以上的列,你可以将列名列表传递给方括号,如下所示。
myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
column_names=["Class","Name"]
column=myDf[column_names]
print("The {} column is :{}".format(column_names,column))
输出。
The dataframe is:
Class Roll Name
0 1 11 Aditya
1 1 12 Chris
2 1 13 Sam
3 2 1 Joel
4 2 22 Tom
5 2 44 Samantha
6 3 33 Tina
7 3 34 Amy
The ['Class', 'Name'] column is : Class Name
0 1 Aditya
1 1 Chris
2 1 Sam
3 2 Joel
4 2 Tom
5 2 Samantha
6 3 Tina
7 3 Amy
潘达斯数据框架中的布尔掩码
布尔掩码是用来检查数据框架中的一个条件。当我们在数据框架的列上应用布尔运算符时,它会根据条件返回一个包含True 和False 的潘达斯系列对象,如下图所示。
myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
boolean_mask=myDf["Class"]>1
print("The boolean mask is:")
print(boolean_mask)
输出。
The dataframe is:
Class Roll Name
0 1 11 Aditya
1 1 12 Chris
2 1 13 Sam
3 2 1 Joel
4 2 22 Tom
5 2 44 Samantha
6 3 33 Tina
7 3 34 Amy
The boolean mask is:
0 False
1 False
2 False
3 True
4 True
5 True
6 True
7 True
Name: Class, dtype: bool
你可以使用布尔掩码从数据框架中选择行。为此,你需要将包含布尔掩码的系列传递给方括号运算符,如下图所示。
myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
boolean_mask=myDf["Class"]>1
print("The boolean mask is:")
print(boolean_mask)
print("The rows in which class>1 is:")
rows=myDf[boolean_mask]
print(rows)
输出。
The dataframe is:
Class Roll Name
0 1 11 Aditya
1 1 12 Chris
2 1 13 Sam
3 2 1 Joel
4 2 22 Tom
5 2 44 Samantha
6 3 33 Tina
7 3 34 Amy
The boolean mask is:
0 False
1 False
2 False
3 True
4 True
5 True
6 True
7 True
Name: Class, dtype: bool
The rows in which class>1 is:
Class Roll Name
3 2 1 Joel
4 2 22 Tom
5 2 44 Samantha
6 3 33 Tina
7 3 34 Amy
不使用方括号,你也可以使用where() 方法,使用布尔掩码从数据框架中选择行。where() 方法,当在一个数据框架上调用时,将布尔掩码作为其输入参数,并返回一个包含所需行的新数据框架,如下图所示。
myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
boolean_mask=myDf["Class"]>1
print("The boolean mask is:")
print(boolean_mask)
print("The rows in which class>1 is:")
rows=myDf.where(boolean_mask)
print(rows)
输出。
The dataframe is:
Class Roll Name
0 1 11 Aditya
1 1 12 Chris
2 1 13 Sam
3 2 1 Joel
4 2 22 Tom
5 2 44 Samantha
6 3 33 Tina
7 3 34 Amy
The boolean mask is:
0 False
1 False
2 False
3 True
4 True
5 True
6 True
7 True
Name: Class, dtype: bool
The rows in which class>1 is:
Class Roll Name
0 NaN NaN NaN
1 NaN NaN NaN
2 NaN NaN NaN
3 2.0 1.0 Joel
4 2.0 22.0 Tom
5 2.0 44.0 Samantha
6 3.0 33.0 Tina
7 3.0 34.0 Amy
在上述使用where() 方法的例子中,布尔掩码的值为False,NaN 的行被存储在数据框架中。你可以删除包含NaN 值的行,如下图所示。
myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
boolean_mask=myDf["Class"]>1
print("The boolean mask is:")
print(boolean_mask)
print("The rows in which class>1 is:")
rows=myDf.where(boolean_mask).dropna()
print(rows)
输出。
The dataframe is:
Class Roll Name
0 1 11 Aditya
1 1 12 Chris
2 1 13 Sam
3 2 1 Joel
4 2 22 Tom
5 2 44 Samantha
6 3 33 Tina
7 3 34 Amy
The boolean mask is:
0 False
1 False
2 False
3 True
4 True
5 True
6 True
7 True
Name: Class, dtype: bool
The rows in which class>1 is:
Class Roll Name
3 2.0 1.0 Joel
4 2.0 22.0 Tom
5 2.0 44.0 Samantha
6 3.0 33.0 Tina
7 3.0 34.0 Amy
你也可以使用逻辑运算符从两个或多个条件中创建布尔掩码,如下图所示。
myDf=pd.read_csv("samplefile.csv")
print("The dataframe is:")
print(myDf)
boolean_mask=(myDf["Class"]>1) & (myDf["Class"]<3)
print("The boolean mask is:")
print(boolean_mask)
print("The rows in which class>1 and <3 is:")
rows=myDf.where(boolean_mask).dropna()
print(rows)
输出。
The dataframe is:
Class Roll Name
0 1 11 Aditya
1 1 12 Chris
2 1 13 Sam
3 2 1 Joel
4 2 22 Tom
5 2 44 Samantha
6 3 33 Tina
7 3 34 Amy
The boolean mask is:
0 False
1 False
2 False
3 True
4 True
5 True
6 False
7 False
Name: Class, dtype: bool
The rows in which class>1 and <3 is:
Class Roll Name
3 2.0 1.0 Joel
4 2.0 22.0 Tom
5 2.0 44.0 Samantha
在创建布尔掩码后,你可以用它来选择布尔掩码包含 "真 "的行,如下图所示。
总结
在这篇文章中,我们讨论了如何从数据框架中选择一条记录。我们还讨论了如何从一个数据框架中选择一列,以及如何使用布尔掩码从一个数据框架中选择多条记录。