Dataframe tail method return last n rows of the dataframe.
Example
df.tail()
If you do not pass any argument to the tail method, then it return last 5 row.
df.tail(3)
Above snippet return last 3 rows.
For negative values of `n`, this function returns all rows except the first `n` rows.
except_first_2_rows = df.tail(-2)
Find the below working application.
last_n_rows.py
import pandas as pd
# Create a sample DataFrame
data = {'Name': ['Krishna', 'Ram', 'Joel', 'Gopi', 'Jitendra', "Raj"],
'Age': [34, 25, 29, 41, 52, 23],
'City': ['Bangalore', 'Chennai', 'Hyderabad', 'Hyderabad', 'Bangalore', 'Chennai']}
df = pd.DataFrame(data)
last_5_rows = df.tail()
print("last_5_rows : \n", last_5_rows)
last_3_rows = df.tail(3)
print("\nlast_3_rows : \n", last_3_rows)
except_first_2_rows = df.tail(-2)
print("\nexcept_first_2_rows : \n", except_first_2_rows)
Output
last_5_rows :
Name Age City
1 Ram 25 Chennai
2 Joel 29 Hyderabad
3 Gopi 41 Hyderabad
4 Jitendra 52 Bangalore
5 Raj 23 Chennai
last_3_rows :
Name Age City
3 Gopi 41 Hyderabad
4 Jitendra 52 Bangalore
5 Raj 23 Chennai
except_first_2_rows :
Name Age City
2 Joel 29 Hyderabad
3 Gopi 41 Hyderabad
4 Jitendra 52 Bangalore
5 Raj 23 Chennai
No comments:
Post a Comment