VOOZH about

URL: https://www.geeksforgeeks.org/pandas/python-pandas-crud-operations/

⇱ Pandas CRUD Operations - Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Pandas CRUD Operations - Python

Last Updated : 26 Jul, 2025

CRUD stands for Create, Read, Update and Delete. These are the four fundamental operations we'll use when working with data in Pandas. Whether we're creating a DataFrame from scratch, analyzing existing data, modifying values or saving our results these operations are important in Pandas.

Let’s see each operation step by step to see how they make data manipulation easy.

1. Create: Creating Dataframe

Creating a dataset in Pandas means building a DataFrame which is the main data structure in Pandas. We can create a DataFrame using various methods like reading from a file or directly creating one from Python objects like dictionaries, lists or arrays

1. Creating a DataFrame from a Dictationary

This is one of the easiest and most commonly used methods to create a dataset in Pandas

Output:

πŸ‘ crud1
DataFrame from a Dictationary

2. Creating a DataFrame from Lists

We can also create a DataFrame by combining lists.

Output:

πŸ‘ crud2
DataFrame from Lists

3. Creating a DataFrame from a CSV File

We can also create a DataFrame by reading an external file like a CSV. Here we used the random car.csv data.

You can download dataset from here.

Output:

πŸ‘ crud3
DataFrame from a CSV File

2. Read: Reading Dataframe

Now that we’ve created a dataset using the Create operation, lets see by using the Read operation. This step is all about accessing and understanding our data. Pandas provides simple methods to view our dataset, check its structure and analyze its contents.

1. Viewing Rows in a DataFrame

  • head(n): Displaying the First Few Rows
  • tail(n): Displaying the Last Few Rows

Output:

πŸ‘ crud4
Viewing Rows in a DataFrame

2. Exploring Columns of the dataset

Output:

Index(['Name', 'Age', 'City'], dtype='object')

3. Checking Data Types with dtype

We use df.types to check the particular data type of the columns we have for further operations

Output:

πŸ‘ crud6
dtype()

4. Generating Descriptive Statistics with describe()

This is a very important command used in pandas to check the overall statistics for the numerical data so that we can make predictions and move easily in our data exploration.

Output:

πŸ‘ crud7
describe()

3. Filtering Columns

Accessing a single Column.

Output:

πŸ‘ crud8
Single column

4. Accessing Multiple columns

Output:

πŸ‘ crud9
Multiple columns

5. Finding Unique Values in a Column

Finding unique tends to provide the non-duplicates values in our columns.


Output:

['NY' 'LA' 'SF' 'Houston' 'Seattle']

6. Filtering Rows (Conditional Filtering)

Single Condition Filtering.

Output:

πŸ‘ crud11
Filtering Rows

7. Filtering with Multiple Conditions (AND/OR Logic)

Output:

πŸ‘ crud12
Filtering with Multiple Conditions

8. Indexing in Pandas

Integer-Based Indexing with iloc.


Output:

πŸ‘ crud13
Integer-Based Indexing
9. Accessing Rows and Cells


Output:

NY

10. Slicing Rows

Output:

πŸ‘ crud15
Slicing Rows

11. Label-Based Indexing

12. Setting an Index and Accessing Rows by Labels

Output:

πŸ‘ crud16
Accessing Rows by Labels

Now, we will continue with the Update (U) and Delete (D) operations, which are important for modifying and managing data efficiently.

3. Update: Modifying Data in Pandas

Update operation allows us to modify existing data within a DataFrame. Whether we're changing specific values, updating entire columns or applying conditions to update data, Pandas makes it simple.

We will use the following dataset for the update operations.

Output:

πŸ‘ CRUD-A
Modifying Data

1. Updating a Single Value: We can update a single value in a specific row and column using loc or iloc.

Output:

πŸ‘ CRUD-AA
Updating AGE

2. Updating an Entire Column: We can update an entire column by assigning a new list, series or value.

Output:

πŸ‘ CRUD-B
Updating an Entire Column

3. Updating Based on a Condition: We can apply conditions to update values in a DataFrame.

Output:

πŸ‘ CRUD-C

4. Delete: Removing Data in Pandas

Delete operation allows us to remove data from a DataFrame. We can drop rows, columns or specific values providing flexibility in cleaning and manipulating datasets. For the delete operations we will use the dataset below.

Output:

πŸ‘ crud-del
Removing Data in Pandas

1. Delete a Column: We can delete a column using the drop() method.

Output:

πŸ‘ CRUD-E
Delete a Column

2. Delete a Row: Similarly we can delete rows by specifying the index.

Output:

πŸ‘ CRUD-DELL
Delete rows by specifying index.

3. Delete Rows Based on Condition: We can delete rows based on conditions.

Output:

πŸ‘ CRUDQ
Delete Rows Based on Condition

4. Delete entire dataset: To delete the entire DataFrame, we can use the del statement or reassign it to an empty DataFrame.

It will return nothing as it empty the dataset. With these basic CRUD operations we can perform data manipulation easily in complex data manipulation tasks in Pandas.

Comment
Article Tags:
Article Tags:

Explore