Day 5-7 of 100 Days of ML Code: Data Manipulation with Pandas
Tags: machinelearning, codenewbie, womenintech, 100daysofcode
Published: 2023-03-17 17:20:43

> "Embrace the struggle, trust the process, and persist with effort."

To start using the Pandas library, we need to import it into our Python environment. For this example, I downloaded a Shop Customer Data dataset from Kaggle that provides a detailed analysis of an imaginary shop's ideal customers. To read the CSV file into our Pandas DataFrame, we can use the pd.read_csv() function.

**Indexing**

Indexing in Pandas refers to the process of selecting specific rows and/or columns from a Pandas DataFrame or Series. There are several ways to perform indexing in Pandas:

iloc(): This method allows you to select rows and columns by integer location.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xhwnd6ffd6my01usg5c.png)

loc(): This method allows you to select rows and columns by label or boolean mask. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t93ij5eh7vgge19qmerq.png)

Boolean Indexing: This method allows you to filter a DataFrame by a boolean condition.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s3rqqzkpww0yihgcw1z7.png)

**Filtering**

Filtering in Pandas refers to the process of selecting a subset of data from a DataFrame based on certain conditions.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/udih16yuwkpq5i2xnams.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tao7xu2cqqrv0rdifzx2.png)

**Updating Rows and Columns**

Updating rows and columns in a Pandas DataFrame involves changing the values of specific cells, rows, or columns.

Updating a specific cell:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1x440pmorey0dg9binc.png)

Updating a specific row:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/215p9a58k3ie14vq5257.png)

Updating a specific column:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bteqwr44bmlniuzwod8.png)

_In Pandas, there are two methods along with filter that are commonly used for transforming data:_

apply: applies a function to a DataFrame or a Series along an axis. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69ix2zlo0zbzasg9teac.png)

map: applies a function to each element of a Series. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1n57xt8zek4h1igftm9.png)

**Adding/ Removing Rows and Columns**

append(): You can use this method to add one or more rows to an existing DataFrame. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yz80se1y2jalrr54hydm.png)

drop(): You can use this method to remove one or more rows from a DataFrame.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lsuo3tg5tk3i8w7hznug.png)

**Sorting Data**

Sorting data is an important operation in data analysis. In Pandas, you can sort data using the sort_values() method.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hsstz53softo4v661mxe.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ouaukvkq3id2midhoi9e.png)

**Grouping and Aggregating Data**

Grouping and aggregating data is a common operation in data analysis. In Pandas, you can group data using the groupby() method and aggregate it using the agg() method.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/782655vwmgl6v47zwmi6.png)