Pandas Dataframe/Series.head() method
×


Pandas Dataframe/Series.head() method

729

Understanding the Pandas head() Method

The head() method in Pandas is a convenient function that allows you to quickly preview the first few rows of a DataFrame or Series. By default, it returns the top 5 rows, but you can specify a different number to suit your needs. This method is invaluable when you're working with large datasets and want to get a quick glimpse of the data structure without loading the entire dataset into memory.

Syntax

DataFrame.head(n=5)

Parameters:

  • n: int, default 5 — The number of rows to return from the top. If not specified, it returns the first 5 rows.

Returns:

  • DataFrame — A DataFrame containing the top n rows.

Example Usage

import pandas as pd

# Sample DataFrame
data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva', 'Frank', 'Grace'],
    'Age': [25, 30, 35, 40, 22, 29, 31],
    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix', 'Philadelphia', 'San Antonio']
}

df = pd.DataFrame(data)

# Display the first 5 rows
print(df.head())

# Display the first 3 rows
print(df.head(3))

Output:

     Name  Age           City
0   Alice   25       New York
1     Bob   30    Los Angeles
2  Charlie   35        Chicago
3    David   40        Houston
4     Eva   22        Phoenix

Using head() with Series

The head() method can also be applied to a Pandas Series to view the first few elements:

# Sample Series
ages = pd.Series([25, 30, 35, 40, 22, 29, 31])

# Display the first 5 elements
print(ages.head())

# Display the first 3 elements
print(ages.head(3))

Output:

0    25
1    30
2    35
3    40
4    22
dtype: int64

Practical Applications

  • Quick Data Inspection: Use head() to quickly inspect the first few rows of your dataset to understand its structure.
  • Data Cleaning: Before performing data cleaning operations, it's useful to check the initial rows to identify any obvious issues.
  • Debugging: When writing data processing code, head() can help you verify that your transformations are working as expected on a small subset of the data.

Conclusion

The head() method is a simple yet powerful tool in the Pandas library that aids in quickly previewing the top rows of a DataFrame or Series. It's especially useful when dealing with large datasets, allowing you to get a sense of the data without having to load the entire dataset into memory. By mastering this method, you can streamline your data exploration and cleaning processes, making your data analysis workflow more efficient.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Unlimited Video Generation

Best Platform to generate videos

Search and buy from Namecheap

Secure Domain for a Minimum Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat