# Data Manipulation with python pandas


<noscript><img alt="Image for post" class="ef dv dr gp w" src="https://miro.medium.com/max/11570/0*aHKnUhdWdBriqfj_" width="5785" height="3862" srcSet="https://miro.medium.com/max/552/0*aHKnUhdWdBriqfj_ 276w, https://miro.medium.com/max/1104/0*aHKnUhdWdBriqfj_ 552w, https://miro.medium.com/max/1280/0*aHKnUhdWdBriqfj_ 640w, https://miro.medium.com/max/1400/0*aHKnUhdWdBriqfj_ 700w" sizes="700px"/></noscript>

Photo by [Sophie Elvis](https://unsplash.com/@thetechnomaid?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)

Data has to be manipulated and cleaned so that it can provide useful insights. Data manipulation is a necessity as there is an increasing amount of data being stored and used.

This article explains some of the data manipulation operations that can help with organizing our data and extracting useful insights.

Pandas, as explained [here](https://medium.com/better-programming/top-10-python-libraries-for-data-science-21e6cd95ca55), is an open-source pyth<span id="rmm"><span id="rmm">o</span></span>n library that implements easy, efficient, high-performance data analysis tools. Pandas provide efficient access to data [wrangling](https://medium.com/better-programming/data-wrangling-with-pandas-57f7f72fe73c)/munging tasks that occupy almost 80 percent of a data scientist’s time. There are different ways to store data for analysis: rectangular data or tabular data containing rows and columns is the most common form.

Tabular data is represented as a Dataframe object in pandas. Every value within a column of the Dataframe has the same data type, either text or numeric but different columns can contain different data types. Dataframes can be created in various ways, like passing in a dictionary, list of lists, reading from a flat-file such as CSV.

![Image for post](https://miro.medium.com/max/60/1*1V9E9_0qb526aFR43Xi-VA.png?q=20)

<noscript><img alt="Image for post" class="ef dv dr gp w" src="https://miro.medium.com/max/1220/1*1V9E9_0qb526aFR43Xi-VA.png" width="610" height="252" srcSet="https://miro.medium.com/max/552/1*1V9E9_0qb526aFR43Xi-VA.png 276w, https://miro.medium.com/max/1104/1*1V9E9_0qb526aFR43Xi-VA.png 552w, https://miro.medium.com/max/1220/1*1V9E9_0qb526aFR43Xi-VA.png 610w" sizes="610px"/></noscript>

How to Install, import pandas, and explore the data has been shown [here](https://medium.com/better-programming/data-wrangling-with-pandas-57f7f72fe73c).

<span class="in fq bn io ip iq"></span><span class="in fq bn io ip iq"></span><span class="in fq bn io ip"></span>

**Sorting**

Sorting is one of the two most important ways to find interesting parts in the Dataframes. `sort_values()`sorts rows. When the column name is passed into the method, the data by default gets sorted in ascending order. `ascending` is set to `False` to sort in descending order. When a list of columns is passed to the `sort_values()`method to sort rows, `ascending`is set to a list of booleans corresponding to the number of the columns to sort in different orders.

![Image for post](https://miro.medium.com/max/60/1*bHYGc8cAjhBKF5A0hfvx_g.png?q=20)

<noscript><img alt="Image for post" class="ef dv dr gp w" src="https://miro.medium.com/max/1532/1*bHYGc8cAjhBKF5A0hfvx_g.png" width="766" height="117" srcSet="https://miro.medium.com/max/552/1*bHYGc8cAjhBKF5A0hfvx_g.png 276w, https://miro.medium.com/max/1104/1*bHYGc8cAjhBKF5A0hfvx_g.png 552w, https://miro.medium.com/max/1280/1*bHYGc8cAjhBKF5A0hfvx_g.png 640w, https://miro.medium.com/max/1400/1*bHYGc8cAjhBKF5A0hfvx_g.png 700w" sizes="700px"/></noscript>

<span class="in fq bn io ip iq"></span><span class="in fq bn io ip iq"></span><span class="in fq bn io ip"></span>

**Subsetting**

A large part of data science is about finding which interesting bits in your dataset. Simple techniques, sometimes known as filtering or selecting rows, are used to find a subset of rows that match some criteria. We can filter single, multiple columns, and text data.

There are many ways to subset a DataFrame: the most common is using relational operators to return `True`or `False` for each row, then passing them into square brackets.

![Image for post](https://miro.medium.com/max/60/1*pUaMZF3dbfOlmakHrm75BA.png?q=20)

<noscript><img alt="Image for post" class="ef dv dr gp w" src="https://miro.medium.com/max/1806/1*pUaMZF3dbfOlmakHrm75BA.png" width="903" height="247" srcSet="https://miro.medium.com/max/552/1*pUaMZF3dbfOlmakHrm75BA.png 276w, https://miro.medium.com/max/1104/1*pUaMZF3dbfOlmakHrm75BA.png 552w, https://miro.medium.com/max/1280/1*pUaMZF3dbfOlmakHrm75BA.png 640w, https://miro.medium.com/max/1400/1*pUaMZF3dbfOlmakHrm75BA.png 700w" sizes="700px"/></noscript>

we can subset rows by creating a logical condition to filter against, the result is a column of booleans

![Image for post](https://miro.medium.com/max/60/1*WKwwQDJM875GBhoLW14-tg.png?q=20)

<noscript><img alt="Image for post" class="ef dv dr gp w" src="https://miro.medium.com/max/1962/1*WKwwQDJM875GBhoLW14-tg.png" width="981" height="106" srcSet="https://miro.medium.com/max/552/1*WKwwQDJM875GBhoLW14-tg.png 276w, https://miro.medium.com/max/1104/1*WKwwQDJM875GBhoLW14-tg.png 552w, https://miro.medium.com/max/1280/1*WKwwQDJM875GBhoLW14-tg.png 640w, https://miro.medium.com/max/1400/1*WKwwQDJM875GBhoLW14-tg.png 700w" sizes="700px"/></noscript>

![Image for post](https://miro.medium.com/max/60/1*Do4ByNJi7hx_GIiP1lECtA.png?q=20)

<noscript><img alt="Image for post" class="ef dv dr gp w" src="https://miro.medium.com/max/1172/1*Do4ByNJi7hx_GIiP1lECtA.png" width="586" height="57" srcSet="https://miro.medium.com/max/552/1*Do4ByNJi7hx_GIiP1lECtA.png 276w, https://miro.medium.com/max/1104/1*Do4ByNJi7hx_GIiP1lECtA.png 552w, https://miro.medium.com/max/1172/1*Do4ByNJi7hx_GIiP1lECtA.png 586w" sizes="586px"/></noscript>

we can filter on multiple conditions by using logical operators, the bitwise ‘and’/ampersand(&) and ‘or’/pipe (|)

![Image for post](https://miro.medium.com/max/60/1*5GoyhpS5x5qqrlL18jBMHw.png?q=20)

<noscript><img alt="Image for post" class="ef dv dr gp w" src="https://miro.medium.com/max/1386/1*5GoyhpS5x5qqrlL18jBMHw.png" width="693" height="226" srcSet="https://miro.medium.com/max/552/1*5GoyhpS5x5qqrlL18jBMHw.png 276w, https://miro.medium.com/max/1104/1*5GoyhpS5x5qqrlL18jBMHw.png 552w, https://miro.medium.com/max/1280/1*5GoyhpS5x5qqrlL18jBMHw.png 640w, https://miro.medium.com/max/1386/1*5GoyhpS5x5qqrlL18jBMHw.png 693w" sizes="693px"/></noscript>

<span class="in fq bn io ip iq"></span><span class="in fq bn io ip iq"></span><span class="in fq bn io ip"></span>

**New columns**

We might need to create a new column from the existing columns. Creating a new column can also be called mutating a Dataframe, transforming a Dataframe, and feature engineering.

![Image for post](https://miro.medium.com/max/60/1*npjfBo4YQAq6Gm8OYhqDrQ.png?q=20)

<noscript><img alt="Image for post" class="ef dv dr gp w" src="https://miro.medium.com/max/1824/1*npjfBo4YQAq6Gm8OYhqDrQ.png" width="912" height="306" srcSet="https://miro.medium.com/max/552/1*npjfBo4YQAq6Gm8OYhqDrQ.png 276w, https://miro.medium.com/max/1104/1*npjfBo4YQAq6Gm8OYhqDrQ.png 552w, https://miro.medium.com/max/1280/1*npjfBo4YQAq6Gm8OYhqDrQ.png 640w, https://miro.medium.com/max/1400/1*npjfBo4YQAq6Gm8OYhqDrQ.png 700w" sizes="700px"/></noscript>

![Image for post](https://miro.medium.com/max/60/1*Ja2e2xqlee89GeRfMlj1Ug.jpeg?q=20)

<noscript><img alt="Image for post" class="ef dv dr gp w" src="https://miro.medium.com/max/1902/1*Ja2e2xqlee89GeRfMlj1Ug.jpeg" width="951" height="326" srcSet="https://miro.medium.com/max/552/1*Ja2e2xqlee89GeRfMlj1Ug.jpeg 276w, https://miro.medium.com/max/1104/1*Ja2e2xqlee89GeRfMlj1Ug.jpeg 552w, https://miro.medium.com/max/1280/1*Ja2e2xqlee89GeRfMlj1Ug.jpeg 640w, https://miro.medium.com/max/1400/1*Ja2e2xqlee89GeRfMlj1Ug.jpeg 700w" sizes="700px"/></noscript>

From our data,we can confirm by checking the shape of the new column. The number of columns increased by one from 12 to 13.

![Image for post](https://miro.medium.com/max/60/1*GHFEt4ASv8ocmWlK0r8J3Q.png?q=20)

<noscript><img alt="Image for post" class="ef dv dr gp w" src="https://miro.medium.com/max/850/1*GHFEt4ASv8ocmWlK0r8J3Q.png" width="425" height="101" srcSet="https://miro.medium.com/max/552/1*GHFEt4ASv8ocmWlK0r8J3Q.png 276w, https://miro.medium.com/max/850/1*GHFEt4ASv8ocmWlK0r8J3Q.png 425w" sizes="425px"/></noscript>

![Image for post](https://miro.medium.com/max/60/1*voGWcz6u4ZRaQl9iuWyMlQ.png?q=20)

<noscript><img alt="Image for post" class="ef dv dr gp w" src="https://miro.medium.com/max/1322/1*voGWcz6u4ZRaQl9iuWyMlQ.png" width="661" height="99" srcSet="https://miro.medium.com/max/552/1*voGWcz6u4ZRaQl9iuWyMlQ.png 276w, https://miro.medium.com/max/1104/1*voGWcz6u4ZRaQl9iuWyMlQ.png 552w, https://miro.medium.com/max/1280/1*voGWcz6u4ZRaQl9iuWyMlQ.png 640w, https://miro.medium.com/max/1322/1*voGWcz6u4ZRaQl9iuWyMlQ.png 661w" sizes="661px"/></noscript>

<span class="in fq bn io ip iq"></span><span class="in fq bn io ip iq"></span><span class="in fq bn io ip"></span>

We have seen the four most common types of data manipulation: sorting rows, subsetting columns, subsetting rows, and adding new columns. What other data manipulations operations do you know?
