Python Pandas: How to View the Last Rows of a DataFrame using 'tail()'
This Python code snippet uses the 'pandas' library to access the final 5 rows of a DataFrame. The 'tail()' function in Pandas allows you to retrieve the last 'n' rows of a DataFrame, with the default value being 5. Here's a breakdown:
test_df.tail()
Explanation:
test_df: This represents your Pandas DataFrame containing the data you want to analyze..tail(): This is a Pandas DataFrame method that specifically extracts the last rows of a DataFrame.(): You can optionally pass an integer argument within the parentheses to specify the number of rows you wish to display. For example,test_df.tail(10)would retrieve the last 10 rows.
Example:
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'Age': [25, 30, 22, 28, 35]}
df = pd.DataFrame(data)
print(df.tail())
Output:
Name Age
3 David 28
4 Eve 35
This output shows the last two rows of the DataFrame 'df', highlighting the utility of 'tail()' for quickly examining the end of a dataset.
原文地址: https://www.cveoy.top/t/topic/n8YF 著作权归作者所有。请勿转载和采集!