To drop the last row of a pandas DataFrame, the easiest way is with the iloc.
import pandas as pd
df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
'Weight': [130.54, 160.20, 209.45, 150.35, 117.73, 187.52],
'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42],
'Age': [43,23,71,49,52,37] })
print(df)
print(df.iloc[:-3])
#Output:
Name Weight Height Age
0 Jim 130.54 50.10 43
1 Sally 160.20 68.94 23
2 Bob 209.45 71.42 71
3 Sue 150.35 48.56 49
4 Jill 117.73 59.37 52
5 Larry 187.52 63.42 37
Name Weight Height Age
0 Jim 130.54 50.10 43
1 Sally 160.20 68.94 23
2 Bob 209.45 71.42 71
You can also use the pandas DataFrame drop() function to drop the last n rows of a DataFrame.
import pandas as pd
df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
'Weight': [130.54, 160.20, 209.45, 150.35, 117.73, 187.52],
'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42],
'Age': [43,23,71,49,52,37] })
print(df)
print(df.drop(df.index[-3:]))
#Output:
Name Weight Height Age
0 Jim 130.54 50.10 43
1 Sally 160.20 68.94 23
2 Bob 209.45 71.42 71
3 Sue 150.35 48.56 49
4 Jill 117.73 59.37 52
5 Larry 187.52 63.42 37
Name Weight Height Age
0 Jim 130.54 50.10 43
1 Sally 160.20 68.94 23
2 Bob 209.45 71.42 71
One last way you can get rid of the last n rows is with the pandas DataFrame head() function.
import pandas as pd
df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
'Weight': [130.54, 160.20, 209.45, 150.35, 117.73, 187.52],
'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42],
'Age': [43,23,71,49,52,37] })
print(df)
print(df.head(-3))
#Output:
Name Weight Height Age
0 Jim 130.54 50.10 43
1 Sally 160.20 68.94 23
2 Bob 209.45 71.42 71
3 Sue 150.35 48.56 49
4 Jill 117.73 59.37 52
5 Larry 187.52 63.42 37
Name Weight Height Age
0 Jim 130.54 50.10 43
1 Sally 160.20 68.94 23
2 Bob 209.45 71.42 71
When working with data, the ability to remove and filter certain segments of your data easily is valuable.
One such case is if you want to remove the last n rows of a pandas DataFrame.
There are a few ways you can remove the last n rows of a pandas DataFrame.
These ways include using the pandas DataFrame iloc property, the pandas drop() Function and pandas tail() Function.
The rest of this post shows these three ways of dropping the last n rows of a pandas DataFrame.
Using iloc to Drop Last n Rows of pandas DataFrame
The first way to get rid of the last n rows of a pandas DataFrame is with iloc.
The pandas DataFrame iloc property is a purely integer-location based indexing for selection by position.
With iloc, you can easily select rows that you want to work with.
To drop the last n rows of a pandas DataFrame with iloc, you can select everything except the last n rows by passing “[-n:]”.
Below shows you how to use iloc to drop the last n rows in a pandas DataFrame.
import pandas as pd
df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
'Weight': [130.54, 160.20, 209.45, 150.35, 117.73, 187.52],
'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42],
'Age': [43,23,71,49,52,37] })
print(df)
print(df.iloc[-3:])
#Output:
Name Weight Height Age
0 Jim 130.54 50.10 43
1 Sally 160.20 68.94 23
2 Bob 209.45 71.42 71
3 Sue 150.35 48.56 49
4 Jill 117.73 59.37 52
5 Larry 187.52 63.42 37
Name Weight Height Age
0 Jim 130.54 50.10 43
1 Sally 160.20 68.94 23
2 Bob 209.45 71.42 71
Using drop() to Remove Last n Rows of pandas DataFrame
Another way you can drop the last n rows of a pandas DataFrame is with the pandas drop() function.
drop() allows you to drop both rows and columns. By default, you can use drop() to delete rows of a pandas DataFrame.
To remove the last n rows from a pandas DataFrame, pass ‘df.index[-n:]’ to drop().
Below shows you how you can use drop() to remove the last n rows from a pandas DataFrame.
import pandas as pd
df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
'Weight': [130.54, 160.20, 209.45, 150.35, 117.73, 187.52],
'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42],
'Age': [43,23,71,49,52,37] })
print(df)
print(df.drop(df.index[-1:]))
#Output:
Name Weight Height Age
0 Jim 130.54 50.10 43
1 Sally 160.20 68.94 23
2 Bob 209.45 71.42 71
3 Sue 150.35 48.56 49
4 Jill 117.73 59.37 52
5 Larry 187.52 63.42 37
Name Weight Height Age
0 Jim 130.54 50.10 43
1 Sally 160.20 68.94 23
2 Bob 209.45 71.42 71
Using head() to Drop Last n Rows of pandas DataFrame
One last way you can drop the last row of a pandas DataFrame is with the pandas head() function.
The pandas head() function allows us to get the first n rows of our DataFrame. By default, n is 5, but you can change this to any valid integer.
If you pass a negative number to head(), you will get everything except the last n rows.
Below shows you how to use the head() function to get rid of the last n rows of a pandas DataFrame.
import pandas as pd
df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
'Weight': [130.54, 160.20, 209.45, 150.35, 117.73, 187.52],
'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42],
'Age': [43,23,71,49,52,37] })
print(df)
print(df.head(-1))
#Output:
Name Weight Height Age
0 Jim 130.54 50.10 43
1 Sally 160.20 68.94 23
2 Bob 209.45 71.42 71
3 Sue 150.35 48.56 49
4 Jill 117.73 59.37 52
5 Larry 187.52 63.42 37
Name Weight Height Age
0 Jim 130.54 50.10 43
1 Sally 160.20 68.94 23
2 Bob 209.45 71.42 71
Hopefully this article has been useful for you to learn how to remove the last n rows of a pandas DataFrame in your Python code.