📜  删除 Pandas DataFrame 的最后 n 行

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

删除 Pandas DataFrame 的最后 n 行

让我们看看删除 Pandas Dataframe 的最后 n 行的各种方法。
首先,让我们制作一个数据框:

Python3
# Import Required Libraries
import pandas as pd
 
# Create a dictionary for the dataframe
dict = {
  'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel',
           'Sanskriti', 'Abhishek Jain'],
  'Age': [22, 20, 45, 21, 22],
   'Marks': [90, 84, -33, -87, 82]
}
 
# Converting Dictionary to
# Pandas Dataframe
df = pd.DataFrame(dict)
 
# Print Dataframe
print(df)


Python3
# Import Required Libraries
import pandas as pd
 
# Create a dictionary for the dataframe
dict = {
  'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel',
           'Sanskriti', 'Abhishek Jain'],
  'Age': [22, 20, 45, 21, 22],
   'Marks': [90, 84, -33, -87, 82]
}
 
# Converting Dictionary to
# Pandas Dataframe
df = pd.DataFrame(dict)
 
# Number of rows to drop
n = 3
 
# Dropping last n rows using drop
df.drop(df.tail(n).index,
        inplace = True)
 
# Printing dataframe
print(df)


Python3
# Import Required Libraries
import pandas as pd
 
# Create a dictionary for the dataframe
dict = {
  'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel',
           'Sanskriti', 'Abhishek Jain'],
  'Age': [22, 20, 45, 21, 22],
   'Marks': [90, 84, -33, -87, 82]
}
 
# Converting Dictionary to
# Pandas Dataframe
df = pd.DataFrame(dict)
 
# Number of rows to drop
n = 3
 
# Removing last n rows
df_dropped_last_n = df.iloc[:-n]
 
# Printing dataframe
print(df_dropped_last_n)


Python3
# Import Required Libraries
import pandas as pd
 
# Create a dictionary for the dataframe
dict = {
  'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel',
           'Sanskriti', 'Abhishek Jain'],
  'Age': [22, 20, 45, 21, 22],
   'Marks': [90, 84, -33, -87, 82]
}
 
# Converting Dictionary to
# Pandas Dataframe
df = pd.DataFrame(dict)
 
# Number of rows to drop
n = 3
 
# Using head() to
# drop last n rows
df1 = df.head(-n)
 
# Printing dataframe
print(df1)


Python3
# Import Required Libraries
import pandas as pd
 
# Create a dictionary for the dataframe
dict = {
  'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel',
           'Sanskriti', 'Abhishek Jain'],
  'Age': [22, 20, 45, 21, 22],
   'Marks': [90, 84, -33, -87, 82]
}
 
# Converting Dictionary to
# Pandas Dataframe
df = pd.DataFrame(dict)
 
# Number of rows to drop
n = 3
 
# Slicing last n rows
df1 = df[:-n]
 
# Printing dataframe
print(df1)


输出:

方法 1:使用Dataframe.drop()
我们可以使用 drop() 方法删除最后 n 行。 drop() 方法获取一个 inplace 参数,该参数采用布尔值。如果 inplace 属性设置为 True,则数据框将使用数据框的新值(删除了最后 n 行的数据框)进行更新。

例子:

Python3

# Import Required Libraries
import pandas as pd
 
# Create a dictionary for the dataframe
dict = {
  'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel',
           'Sanskriti', 'Abhishek Jain'],
  'Age': [22, 20, 45, 21, 22],
   'Marks': [90, 84, -33, -87, 82]
}
 
# Converting Dictionary to
# Pandas Dataframe
df = pd.DataFrame(dict)
 
# Number of rows to drop
n = 3
 
# Dropping last n rows using drop
df.drop(df.tail(n).index,
        inplace = True)
 
# Printing dataframe
print(df)

输出:

方法二:使用 数据框.iloc[]

当数据框的索引标签不是数字系列 0、1、2、3….n 或用户不知道索引标签时,使用方法。

例子:

Python3

# Import Required Libraries
import pandas as pd
 
# Create a dictionary for the dataframe
dict = {
  'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel',
           'Sanskriti', 'Abhishek Jain'],
  'Age': [22, 20, 45, 21, 22],
   'Marks': [90, 84, -33, -87, 82]
}
 
# Converting Dictionary to
# Pandas Dataframe
df = pd.DataFrame(dict)
 
# Number of rows to drop
n = 3
 
# Removing last n rows
df_dropped_last_n = df.iloc[:-n]
 
# Printing dataframe
print(df_dropped_last_n)

输出:

方法3:使用 数据框.head()

此方法用于返回数据框或系列的前 n 行(默认为 5 行)。

例子:

Python3

# Import Required Libraries
import pandas as pd
 
# Create a dictionary for the dataframe
dict = {
  'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel',
           'Sanskriti', 'Abhishek Jain'],
  'Age': [22, 20, 45, 21, 22],
   'Marks': [90, 84, -33, -87, 82]
}
 
# Converting Dictionary to
# Pandas Dataframe
df = pd.DataFrame(dict)
 
# Number of rows to drop
n = 3
 
# Using head() to
# drop last n rows
df1 = df.head(-n)
 
# Printing dataframe
print(df1)

输出:

方法 4:使用Dataframe 切片 [ ]。

例子:

Python3

# Import Required Libraries
import pandas as pd
 
# Create a dictionary for the dataframe
dict = {
  'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel',
           'Sanskriti', 'Abhishek Jain'],
  'Age': [22, 20, 45, 21, 22],
   'Marks': [90, 84, -33, -87, 82]
}
 
# Converting Dictionary to
# Pandas Dataframe
df = pd.DataFrame(dict)
 
# Number of rows to drop
n = 3
 
# Slicing last n rows
df1 = df[:-n]
 
# Printing dataframe
print(df1)

输出: