Pandas Analyzing Data provides several functions to analyze data quickly and understand a dataset. These functions help you check the first and last records, view column information, identify data types, and calculate basic statistical values. In this chapter, you will learn how to analyze a DataFrame using Pandas.
Viewing the First Rows Using head()
The head() function displays the first five rows of a DataFrame by default.
Syntax:
df.head()
Example:
import pandas as pd
data = {
"Name": ["Amit", "Neha", "Rahul", "Priya", "Karan", "Simran"],
"Age": [21, 22, 20, 23, 24, 21],
"Marks": [85, 92, 78, 88, 95, 82]
}
df = pd.DataFrame(data)
print(df.head())
Output:
Name Age Marks
0 Amit 21 85
1 Neha 22 92
2 Rahul 20 78
3 Priya 23 88
4 Karan 24 95
Explanation: The head() function displays the first five rows and does not display the sixth row.
Displaying a Specific Number of Rows Using head()
You can also specify how many rows you want to display by passing a number inside head().
Example:
print(df.head(3))
Output:
Name Age Marks
0 Amit 21 85
1 Neha 22 92
2 Rahul 20 78
Explanation: head(3) displays only the first three rows of the DataFrame.
Viewing the Last Rows Using tail()
The tail() function displays the last five rows of a DataFrame by default.
Syntax:
df.tail()
Example:
print(df.tail())
Output:
Name Age Marks
1 Neha 22 92
2 Rahul 20 78
3 Priya 23 88
4 Karan 24 95
5 Simran 21 82
Explanation: The tail() function displays the last five rows of the DataFrame.
Displaying a Specific Number of Last Rows Using tail()
You can specify the number of rows you want to display from the end of a DataFrame.
Example:
print(df.tail(2))
Output:
Name Age Marks
4 Karan 24 95
5 Simran 21 82
Explanation: tail(2) displays only the last two rows of the DataFrame.
Getting Information About Data Using info()
The info() function displays important information about a DataFrame, such as column names, number of rows, data types, and memory usage.
Syntax:
df.info()
Example:
df.info()
Output:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6 entries, 0 to 5
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Name 6 non-null object
1 Age 6 non-null int64
2 Marks 6 non-null int64
dtypes: int64(2), object(1)
memory usage: ...
Explanation:
RangeIndex: 6 entriesmeans the DataFrame contains six rows.Data columns (total 3 columns)means there are three columns.Non-Null Countshows how many values are not missing in each column.Dtypeshows the data type of each column.memory usageshows the approximate memory used by the DataFrame.
The exact memory usage may differ depending on your Pandas version and environment.
Getting Statistical Information Using describe()
The describe() function generates statistical information about numerical columns in a DataFrame.
Syntax:
df.describe()
Example:
print(df.describe())
Output:
Age Marks
count 6.000000 6.000000
mean 21.833333 86.666667
std 1.471960 6.218253
min 20.000000 78.000000
25% 21.000000 82.750000
50% 21.500000 86.500000
75% 22.750000 91.000000
max 24.000000 95.000000
Explanation:
The describe() function provides the following statistical values:
| Value | Meaning |
|---|---|
| count | Number of non-missing values |
| mean | Average value |
| std | Standard deviation |
| min | Smallest value |
| 25% | First quartile |
| 50% | Median |
| 75% | Third quartile |
| max | Largest value |
Note: By default, describe() analyzes numerical columns. Text columns can also be included using include="all".
Analyzing All Columns Using describe()
You can use include="all" to get descriptive information about both numerical and non-numerical columns.
Example:
print(df.describe(include="all"))
Output:
Name Age Marks
count 6 6.000000 6.000000
unique 6 NaN NaN
top Amit NaN NaN
freq 1 NaN NaN
mean NaN 21.833333 86.666667
std NaN 1.471960 6.218253
min NaN 20.000000 78.000000
25% NaN 21.000000 82.750000
50% NaN 21.500000 86.500000
75% NaN 22.750000 91.000000
max NaN 24.000000 95.000000
Explanation:
uniqueshows the number of different values.topshows the most frequently occurring value.freqshows how many times the most frequent value appears.NaNmeans a particular statistical value is not applicable to that column.
Checking the Number of Rows and Columns Using shape
The shape attribute returns the number of rows and columns in a DataFrame.
Example:
print(df.shape)
Output:
(6, 3)
Explanation: The result (6, 3) means the DataFrame contains six rows and three columns.
You can also access the row and column counts separately.
Example:
print("Rows:", df.shape[0])
print("Columns:", df.shape[1])
Output:
Rows: 6
Columns: 3
Checking Column Names Using columns
The columns attribute returns the names of all columns in a DataFrame.
Example:
print(df.columns)
Output:
Index(['Name', 'Age', 'Marks'], dtype='object')
Explanation: This attribute helps you identify all available column names in your dataset.
Checking Data Types Using dtypes
The dtypes attribute displays the data type of each column.
Example:
print(df.dtypes)
Output:
Name object
Age int64
Marks int64
dtype: object
Explanation:
Namehas theobjectdata type because it contains text.Agehas theint64data type because it contains integers.Marksalso has theint64data type.
Checking the Total Number of Elements Using size
The size attribute returns the total number of elements in a DataFrame.
Example:
print(df.size)
Output:
18
Explanation: The DataFrame contains six rows and three columns. Therefore, it has 18 elements in total.
Student Task
Create a DataFrame named employees using the following data:
| Name | Department | Age | Salary |
|---|---|---|---|
| Amit | IT | 25 | 35000 |
| Neha | HR | 28 | 40000 |
| Rahul | Sales | 24 | 32000 |
| Priya | IT | 27 | 45000 |
| Karan | Marketing | 30 | 50000 |
| Simran | HR | 26 | 38000 |
Your Tasks:
- Create a Pandas DataFrame using the given employee data.
- Display the first five rows using
head(). - Display the first three rows using
head(3). - Display the last two rows using
tail(2). - Display complete DataFrame information using
info(). - Generate statistical information using
describe(). - Display all column names using
columns. - Check the data types using
dtypes. - Find the total number of rows and columns using
shape. - Find the total number of elements using
size.
Key Takeaways
head()displays the first five rows by default.tail()displays the last five rows by default.info()provides information about columns, data types, and missing values.describe()generates statistical information about a DataFrame.shapereturns the number of rows and columns.columnsdisplays all column names.dtypesdisplays the data type of each column.sizereturns the total number of elements in a DataFrame.
Frequently Asked Questions (FAQs)
1. What is Pandas Analyzing Data in Python?
Pandas Analyzing Data is the process of examining, exploring, and understanding datasets using Python Pandas. It helps identify patterns, trends, missing values, and useful information from data.
2. How to Perform Data Analysis Using Pandas in Python?
You can perform Pandas Data Analysis using functions such as head(), tail(), info(), describe(), and value_counts(). These functions help explore datasets and understand their structure.
3. How to Analyze Data in Pandas DataFrame?
To Analyze Data in Pandas DataFrame, you can use functions like describe() to view statistical information, info() to check column details, and isnull() to identify missing values.
4. Which Functions Are Commonly Used for Pandas Data Analysis?
Commonly used functions for Pandas Data Analysis include head(), tail(), info(), describe(), shape, columns, dtypes, and value_counts(). These functions help summarize and understand datasets.
5. What Is Pandas DataFrame Analysis?
Pandas DataFrame Analysis involves examining tabular data using Pandas DataFrame methods. It helps users check data types, calculate statistics, identify missing values, and understand data patterns.
6. How Can Beginners Learn Pandas Data Analysis?
Beginners can learn Pandas Data Analysis by starting with DataFrames, exploring datasets, checking column information, calculating statistical summaries, and identifying missing values through practical Python examples.
7. Why Is Pandas Important for Data Analysis in Python?
Pandas is important for data analysis in Python because it provides powerful tools for reading, organizing, cleaning, filtering, and analyzing structured datasets. It is widely used in data science and business analytics.
8. What Can We Learn from a Python Pandas Tutorial for Data Analysis?
A Python Pandas Tutorial for Data Analysis teaches you how to explore datasets, analyze DataFrames, calculate statistics, identify missing values, and understand useful information from real-world data.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
