Pandas DataFrame.loc[] 函数
    
    
            Minahil Noor
    2023年1月30日
    
    Pandas
    Pandas DataFrame
    
- 
          
            pandas.DataFrame.loc[]语法
- 
          
            示例代码:DataFrame.loc[]定位特定行的方法
- 
          
            示例代码:DataFrame.loc[]定位多行的方法
- 
          
            示例代码:DataFrame.loc[]方法来定位特定行和列的数据
![Pandas DataFrame.loc[] 函数](/img/Python-Pandas/feature-image---Pandas-DataFrame-DataFrame.loc-Function.webp) 
Python Pandas DataFrame.loc[] 函数根据传递的输入来定位 DataFrame 的值。
pandas.DataFrame.loc[] 语法
DataFrame.loc[]
参数
这个函数不接受任何参数。然而,它有几种输入方法。你可以阅读它这里。
返回
它返回所需的值。
示例代码:DataFrame.loc[] 定位特定行的方法
import pandas as pd
dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)
dataframe1 = dataframe.loc[0]
print("The Row Data is: \n")
print(dataframe1)
输出:
The Original Data frame is: 
   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Row Data is: 
Attendance            60
Name              Olivia
Obtained Marks        90
Name: 0, dtype: object
函数返回了 0 行的数据。
示例代码:DataFrame.loc[] 定位多行的方法
import pandas as pd
dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)
dataframe1 = dataframe.loc[[0, 1, 2]]
print("The Rows are: \n")
print(dataframe1)
输出:
The Original Data frame is: 
   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Rows are: 
   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
现在它已经返回了 DataFrame 的多行数据。
示例代码:DataFrame.loc[] 方法来定位特定行和列的数据
    
import pandas as pd
dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)
dataframe1 = dataframe.loc[0, 'Attendance']
print("The Value is: \n")
print(dataframe1)
输出:
The Original Data frame is: 
   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Value is: 
60
函数返回索引 0 处的 Attendance 值。
        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe