The pandas tail method returns the final specified number of elements from a Series or rows from a DataFrame. With no argument, it returns the last five; an integer makes the count explicit. section 1.3 section 3.17
Use tail when the end of an ordered result answers the question:
- Inspect the final observations with s.tail() or s.tail(5), then calculate on the returned Series, such as s.tail(5).mean(). section 1.3
- Retrieve the most recent record with df.tail(1) when the DataFrame is chronologically ordered and the newest row is last. section 3.17
- Choose the returned shape deliberately: df.tail(1)['value'] produces a one-element Series, while df.tail(1)[['value']] preserves a one-row DataFrame. section 3.17
- Apply tail to ranked or counted results to inspect the lowest entries, as with df['state'].value_counts().tail(5). section 3.21
Compare the final five observations
Given a monthly Series s, compute the average of its final five values with s.tail(5).mean(). Use s.tail().mean() when the default count of five is sufficient. For a single latest market record, use df.tail(1), then select the value column if the date is unnecessary. section 1.3 section 3.17
df.tail(1)['value']
See a one-element Series extracted from the final DataFrame row. section 3.17
df['state'].value_counts().tail(5)
See tail applied to state frequency counts to inspect the five least-represented states. section 3.21
