📜  Python中pandas DataFrame中最后一个元素的访问索引

📅  最后修改于: 2022-05-13 01:55:29.268000             🧑  作者: Mango

Python中pandas DataFrame中最后一个元素的访问索引

在本文中,我们将了解如何访问 pandas Dataframe 中最后一个元素的索引。为此,我们可以使用 Dataframe.iloc、Dataframe.iget 和 Dataframe.index。让我们一一介绍。

Dataframe.iloc – Pandas Dataframe.iloc 用于通过指定索引来检索数据。在Python中,负索引从末尾开始,因此我们可以通过将其索引指定为 -1 来访问数据帧的最后一个元素。

示例1:下面的程序是从整个Dataframe中访问最后一个元素的索引。  

Python3
# import pandas
import pandas as pd
  
# create dataframe
df = pd.DataFrame({'Name': ['Mukul', 'Rohan', 'Rahul', 
                            'Krish', 'Rohit'],
                   'Course': ['BCA', 'MBA', 'MBA', 'BCA',
                              'BBA'],
                   'Address': ['Saharanpur', 'Mohali',
                               'Saharanpur', 'Mohali', 
                               'Noida']})
  
# Display original dataframe
print("Original dataframe")
print(df)
  
# Display last index value of dataframe
# iloc[-1] is return the last element of 
# all columns in DataFrame.
print("value of last index column")
print(df.iloc[-1])


Python3
# import pandas
import pandas as pd
  
# create dataframe
df = pd.DataFrame({'Name': ['Mukul', 'Rohan', 'Rahul',
                            'Krish', 'Rohit'],
                   'Course': ['BCA', 'MBA', 'MBA', 
                              'BCA', 'BBA'],
                   'Address': ['Saharanpur', 'Mohali', 
                               'Saharanpur', 'Mohali', 
                               'Noida']})
  
# Display original dataframe
print("Original dataframe")
print(df)
  
# Display last index value of Address dataframe
print("last index value of Address Column: ", df['Address'].iloc[-1])


Python3
# import pandas
import pandas as pd
  
# create dataframe
df = pd.DataFrame({'Name': ['sanjay', 'suresh',
                            'Rahul', 'Krish', 
                            'vihan'],
                   'Address': ['Haridwar', 'Mohali', 
                               'mohali', 'Mohali', 
                               'saharanpur']})
  
# Display original dataframe
print(" Original dataframe ")
print(df)
  
# Display last index value of 0 index column
print("last index value of 0 index column is ", df.iat[-1, 0])


Python3
# import pandas
import pandas as pd
  
# create dataframe
df = pd.DataFrame({'Name': ['Mukul', 'Rohan', 
                            'Rahul', 'Krish',
                            'Rohit'],
                   'Address': ['Saharanpur', 'Mohali', 
                               'Saharanpur', 'Mohali', 
                               'Noida']})
  
# Display original dataframe
print(" Original dataframe ")
print(df)
  
# Display last index value of dataframe
# iloc[-1] is return the last element of
# all columns in DataFrame.
print(" last index is ", df.index[-1])


输出:

示例 2:

 下面的程序是从特定列访问最后一个元素的索引。

Python3

# import pandas
import pandas as pd
  
# create dataframe
df = pd.DataFrame({'Name': ['Mukul', 'Rohan', 'Rahul',
                            'Krish', 'Rohit'],
                   'Course': ['BCA', 'MBA', 'MBA', 
                              'BCA', 'BBA'],
                   'Address': ['Saharanpur', 'Mohali', 
                               'Saharanpur', 'Mohali', 
                               'Noida']})
  
# Display original dataframe
print("Original dataframe")
print(df)
  
# Display last index value of Address dataframe
print("last index value of Address Column: ", df['Address'].iloc[-1])

输出:

Dataframe.iat()函数——Pandas iat[] 方法用于在传递位置的数据框中返回数据。传递的位置格式为[行中的位置,列中的位置]。此方法与 Pandas iloc[] 的工作方式类似,但 iat[] 仅用于返回单个值,因此比它运行得更快。

示例 3:

在这个例子中,我们将使用 df.iat()函数。

Python3

# import pandas
import pandas as pd
  
# create dataframe
df = pd.DataFrame({'Name': ['sanjay', 'suresh',
                            'Rahul', 'Krish', 
                            'vihan'],
                   'Address': ['Haridwar', 'Mohali', 
                               'mohali', 'Mohali', 
                               'saharanpur']})
  
# Display original dataframe
print(" Original dataframe ")
print(df)
  
# Display last index value of 0 index column
print("last index value of 0 index column is ", df.iat[-1, 0])

输出:

示例 4:

在此示例中,我们使用 df.index()函数以Python语言访问给定数据框的最后一个元素。

Python3

# import pandas
import pandas as pd
  
# create dataframe
df = pd.DataFrame({'Name': ['Mukul', 'Rohan', 
                            'Rahul', 'Krish',
                            'Rohit'],
                   'Address': ['Saharanpur', 'Mohali', 
                               'Saharanpur', 'Mohali', 
                               'Noida']})
  
# Display original dataframe
print(" Original dataframe ")
print(df)
  
# Display last index value of dataframe
# iloc[-1] is return the last element of
# all columns in DataFrame.
print(" last index is ", df.index[-1])

输出: