Unveiling DataFrame: A Look at Essential Pandas Functions

Punyakeerthi BL
3 min readJun 23, 2024

Before proceeding with this article, please read the following for continuation:

Exploring Pandas: Reading CSV Files into DataFrames

This article dives into a set of fundamental functions in Pandas, a powerful Python library for data analysis. By exploring these functions, you’ll gain a deeper understanding of your DataFrame’s structure and content.

1. Unveiling Column Names: df.columns

The df.columns function acts as a name tag inspector, returning a list containing all the column names within your DataFrame [1:2]. Imagine a DataFrame storing information about employees' years of experience and salaries. Running df.columns on this DataFrame would reveal a list like ['years of experience', 'salary'] [1:2].

2. Peeking at Dimensions: df.shape

Curious about the size of your DataFrame? The df.shape function comes in handy! It returns a tuple containing two numbers, representing the number of rows and columns, respectively [2:3]. For instance, if your DataFrame boasts 30 rows and 2 columns, df.shape would return (30, 2) [2:3].

3. Counting the Cells: df.size

How many data points reside within your DataFrame? The df.size function provides the answer! It calculates the total number of cells in your DataFrame [3:4]. Returning to our example with 30 rows and 2 columns, df.size would yield 60, indicating the total number of cells [3:4].

4. Taking a Glimpse at the Beginning: df.head()

The df.head() function offers a sneak peek at the initial rows of your DataFrame. By default, it displays the first 5 rows [4:5]. Want to see a different number of rows? Simply specify the desired number within the parentheses. For example, df.head(2) would unveil the first two rows [4:5].

5. Examining the End: df.tail()

Ever wondered what lurks at the bottom of your DataFrame? The df.tail() function is here to help! Similar to df.head(), it displays the last rows by default (typically 5) [5:6]. You can adjust this by providing a different number within the parentheses. For instance, df.tail(8) would showcase the last eight rows [5:6].

Note: These functions (df.head() and df.tail()) are particularly useful for exploring large DataFrames, allowing you to quickly grasp the data's beginning and end.

6. Unveiling Summary Statistics: df.describe()

The df.describe() function caters specifically to numerical data within your DataFrame. It generates a summary report, providing valuable insights like the count, mean, standard deviation, minimum, and maximum values for each numeric column [6:7]. This function proves instrumental in understanding the central tendencies and variations within your numerical data.

7. Unveiling Data Types and More: df.info()

For a comprehensive overview of your DataFrame’s structure, the df.info() function is your ally. It delivers a detailed report encompassing the number of entries, data types for each column, and the number of non-null values present in each column [7:8]. This information proves valuable in data cleaning and exploration tasks.

In conclusion, these Pandas functions empower you to efficiently explore and comprehend the structure and content of your DataFrames. By incorporating these functions into your data analysis workflow, you’ll gain a sharper understanding of the data you’re working with, paving the way for more informed decisions.

Example:

import pandas as pd
# Sample data
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, 30, 28, 32],
'Salary': [100000, 120000, 95000, 110000]}
# Create DataFrame
df = pd.DataFrame(data)
# Explore using the functions
print(df.columns) # Output: Index(['Name', 'Age', 'Salary'], dtype='object')
print(df.shape) # Output: (4, 3)
print(df.size) # Output: 12
print(df.head())
print(df.tail())
print(df.describe()) # Only applicable for numerical data (Age and Salary in this case)
print(df.info())

If you like this post please follow me on Linked In: Punyakeerthi BL

--

--

Punyakeerthi BL

Passionate Learner in #GenerativeAI|Python| Micro-Service |Springboot | #GenerativeAILearning Talks about #GenerativeAI,#promptengineer, #Microservices