Pandas DataFrames

Introduction

A Pandas DataFrames is a two-dimensional data structure used to store information in rows and columns. It is similar to an Excel worksheet or a database table. In this chapter, we will learn how to create DataFrames, access their data, and understand their basic structure using simple Python examples.

1. What is a Pandas DataFrame?

A DataFrame is a two-dimensional labeled data structure containing rows and columns.

Each column can store a different data type, such as numbers, text, or dates.

For example, a student record table can contain student names, ages, and marks.

NameAgeMarks
Rahul1885
Priya1990
Amit1875
Neha2095

In Pandas, we can create this type of table using the DataFrame() function.

2. Creating a Pandas DataFrame

You can create a DataFrame using the pd.DataFrame() function.

Example:

import pandas as pd

data = {
    "Name": ["Rahul", "Priya", "Amit"],
    "Age": [18, 19, 18],
    "Marks": [85, 90, 75]
}

df = pd.DataFrame(data)

print(df)

Output:

    Name  Age  Marks
0  Rahul   18     85
1  Priya   19     90
2   Amit   18     75

Explanation:

  • data is a Python dictionary containing student information.
  • pd.DataFrame(data) converts the dictionary into a DataFrame.
  • Dictionary keys become column names.
  • Dictionary values become column data.
  • Pandas automatically creates row indexes starting from 0.

3. Creating a DataFrame Using a List

You can also create a DataFrame using a nested list. Each inner list represents one row.

Example:

import pandas as pd

data = [
    ["Rahul", 18, 85],
    ["Priya", 19, 90],
    ["Amit", 18, 75]
]

df = pd.DataFrame(data)

print(df)

Output:

       0   1   2
0  Rahul  18  85
1  Priya  19  90
2   Amit  18  75

Explanation:

  • Each inner list represents one row.
  • Pandas automatically assigns column labels 0, 1, and 2.
  • We can provide meaningful column names using the columns parameter.

4. Naming DataFrame Columns

The columns parameter allows you to assign custom names to DataFrame columns.

Example:

import pandas as pd

data = [
    ["Rahul", 18, 85],
    ["Priya", 19, 90],
    ["Amit", 18, 75]
]

df = pd.DataFrame(
    data,
    columns=["Name", "Age", "Marks"]
)

print(df)

Output:

    Name  Age  Marks
0  Rahul   18     85
1  Priya   19     90
2   Amit   18     75

Explanation:

  • columns defines the column names.
  • The first value in each row belongs to Name.
  • The second value belongs to Age.
  • The third value belongs to Marks.

5. Creating a DataFrame Using a List of Dictionaries

You can create a DataFrame using a list of dictionaries. Each dictionary represents one row, and its keys become column names.

Example:

import pandas as pd

data = [
    {"Name": "Rahul", "Age": 18, "Marks": 85},
    {"Name": "Priya", "Age": 19, "Marks": 90},
    {"Name": "Amit", "Age": 18, "Marks": 75}
]

df = pd.DataFrame(data)

print(df)

Output:

    Name  Age  Marks
0  Rahul   18     85
1  Priya   19     90
2   Amit   18     75

Explanation:

  • Each dictionary represents one student.
  • Dictionary keys become column names.
  • Dictionary values become row values.
  • This method is useful when working with records received from APIs or other data sources.

6. Naming DataFrame Rows Using the index Parameter

By default, Pandas assigns numeric indexes to DataFrame rows. You can use the index parameter to assign custom row labels.

Example:

import pandas as pd

data = {
    "Name": ["Rahul", "Priya", "Amit"],
    "Marks": [85, 90, 75]
}

df = pd.DataFrame(
    data,
    index=["Student1", "Student2", "Student3"]
)

print(df)

Output:

             Name  Marks
Student1    Rahul     85
Student2    Priya     90
Student3     Amit     75

Explanation:

  • index assigns custom labels to DataFrame rows.
  • Student1, Student2, and Student3 replace the default numeric indexes.
  • These labels can be used to identify and access rows.

7. Creating a DataFrame Using the index and columns Parameters

You can use both index and columns to define row labels and column names while creating a DataFrame.

Example:

import pandas as pd

data = [
    [85, 90],
    [75, 80],
    [95, 88]
]

df = pd.DataFrame(
    data,
    index=["Rahul", "Priya", "Amit"],
    columns=["Maths", "Science"]
)

print(df)

Output:

       Maths  Science
Rahul     85       90
Priya     75       80
Amit      95       88

Explanation:

  • index assigns student names as row labels.
  • columns assigns subject names as column labels.
  • Each value is placed at the intersection of its corresponding row and column.

8. Accessing DataFrame Columns

You can access a particular column using its column name inside square brackets.

Example:

import pandas as pd

data = {
    "Name": ["Rahul", "Priya", "Amit"],
    "Age": [18, 19, 18],
    "Marks": [85, 90, 75]
}

df = pd.DataFrame(data)

print(df["Name"])

Output:

0    Rahul
1    Priya
2     Amit
Name: Name, dtype: object

Explanation:

  • df["Name"] selects the Name column.
  • A single selected column is returned as a Pandas Series.
  • The original DataFrame remains unchanged.

9. Accessing Multiple DataFrame Columns

You can select multiple columns by passing a list of column names inside square brackets.

Example:

import pandas as pd

data = {
    "Name": ["Rahul", "Priya", "Amit"],
    "Age": [18, 19, 18],
    "Marks": [85, 90, 75]
}

df = pd.DataFrame(data)

print(df[["Name", "Marks"]])

Output:

    Name  Marks
0  Rahul     85
1  Priya     90
2   Amit     75

Explanation:

  • ["Name", "Marks"] contains the two column names.
  • Pandas returns only those selected columns.
  • The result is another DataFrame.

10. Checking the Number of Rows and Columns

You can use the shape attribute to find the number of rows and columns in a DataFrame.

Example:

import pandas as pd

data = {
    "Name": ["Rahul", "Priya", "Amit"],
    "Age": [18, 19, 18],
    "Marks": [85, 90, 75]
}

df = pd.DataFrame(data)

print(df.shape)

Output:

(3, 3)

Explanation:

The output is a tuple:

  • First value 3 represents the number of rows.
  • Second value 3 represents the number of columns.

Student Task

Create a DataFrame using the following employee information:

Employee IDNameDepartmentSalary
101RahulIT35000
102PriyaHR40000
103AmitSales30000
104NehaIT45000

Complete these tasks:

  1. Create a DataFrame using a dictionary.
  2. Create the same DataFrame using a list of dictionaries.
  3. Assign custom row indexes: Emp1, Emp2, Emp3, and Emp4.
  4. Select only the Name column.
  5. Select the Name and Salary columns.
  6. Check the number of rows and columns using shape.

Key Takeaways

  • A Pandas DataFrame is a two-dimensional data structure.
  • pd.DataFrame() creates a DataFrame.
  • A DataFrame can be created using dictionaries and lists.
  • The columns parameter assigns column names.
  • The index parameter assigns row labels.
  • Single-column selection returns a Series.
  • Multiple-column selection returns a DataFrame.
  • The shape attribute returns the number of rows and columns.

Frequently Asked Questions (FAQs)

1. What are Pandas DataFrames in Python?

Pandas DataFrames are two-dimensional data structures used in Python to store and manage data in rows and columns. They make data analysis, filtering, and manipulation easier.

2. How to Create a Pandas DataFrame in Python?

You can create a Pandas DataFrame using the pd.DataFrame() function. It allows you to create tables from Python dictionaries, lists, NumPy arrays, and other data structures.

3. What is a Python DataFrame?

A Python DataFrame is a tabular data structure provided by the Pandas library. It organizes information into labelled rows and columns, making data handling easier.

4. How do Pandas DataFrames work in Python?

Pandas DataFrames organize data into rows and columns. Each column can contain a different data type, and you can perform operations such as selecting, filtering, updating, and analyzing data.

5. What is the difference between a Pandas Series and a Pandas DataFrame?

A Pandas Series is a one-dimensional data structure, while a Pandas DataFrame is a two-dimensional structure containing multiple rows and columns.

6. Why should beginners learn a Pandas DataFrame Tutorial?

A Pandas DataFrame Tutorial helps beginners understand how to create, access, organize, and manipulate tabular data using Python. These skills are important for learning data analysis and data science.

7. How do you check the shape of a DataFrame in Python?

You can use the shape attribute to check the dimensions of a DataFrame in Python. It returns a tuple containing the total number of rows and columns.

8. What are the main uses of DataFrame in Python?

A DataFrame in Python is commonly used for data cleaning, data filtering, data analysis, handling missing values, organizing datasets, and preparing data for machine learning.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top