

{"id":21110,"date":"2018-07-14T04:10:54","date_gmt":"2018-07-14T04:10:54","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=21110"},"modified":"2026-04-28T15:12:19","modified_gmt":"2026-04-28T09:42:19","slug":"data-wrangling-with-python","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/","title":{"rendered":"Learn Aggregation and Data Wrangling with Python"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:1886,&quot;href&quot;:&quot;https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Data_wrangling&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251009002108\\\/https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Data_wrangling&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-10 06:37:24&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-03 03:41:20&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-12 06:48:22&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-21 07:58:19&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-02 19:59:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-12 12:54:05&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-03-27 15:26:20&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-01 18:39:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-20 14:53:01&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-28 06:35:14&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-22 18:42:13&quot;,&quot;http_code&quot;:429}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-05-22 18:42:13&quot;,&quot;http_code&quot;:429},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>In our previous tutorial, we talked about <a href=\"https:\/\/data-flair.training\/blogs\/python-django-tutorial\/\" target=\"_blank\" rel=\"noopener\"><strong>Python Django<\/strong><\/a>. Today, in this Data Wrangling tutorial, we will see Python Aggregation and Data Wrangling with the Python Programming Language. Moreover, we will discuss p<span id=\":1c6.co\" class=\"tL8wMe EMoHub\" dir=\"ltr\">rerequisites\u00a0 &amp;<\/span><span class=\"tL8wMe EMoHub\" dir=\"ltr\">\u00a0reasons to use Data Wrangling with Python. In addition, we discuss\u00a0<\/span><span id=\":1ba.co\" class=\"tL8wMe EMoHub\" dir=\"ltr\">Dropping Missing Values,\u00a0<\/span><span id=\":1bb.co\" class=\"tL8wMe EMoHub\" dir=\"ltr\">Grouping Data,\u00a0<\/span><span id=\":1bc.co\" class=\"tL8wMe EMoHub\" dir=\"ltr\">Filtering Data,\u00a0<\/span><span id=\":1bd.co\" class=\"tL8wMe EMoHub\" dir=\"ltr\">Pivoting Dataset,<\/span><span id=\":1be.co\" class=\"tL8wMe EMoHub\" dir=\"ltr\"> Shifted Datasets,\u00a0<\/span><span id=\":1bf.co\" class=\"tL8wMe EMoHub\" dir=\"ltr\">Melted Data,\u00a0<\/span><span id=\":1bg.co\" class=\"tL8wMe EMoHub\" dir=\"ltr\">Reducing into an ABT,\u00a0<\/span><span id=\":1bh.co\" class=\"tL8wMe EMoHub\" dir=\"ltr\">Concatenating Data,\u00a0<\/span><span id=\":1bi.co\" class=\"tL8wMe EMoHub\" dir=\"ltr\">Exporting Data, and Python Aggregate.<\/span><\/p>\n<p>So, let&#8217;s start\u00a0Aggregation and Data Wrangling with Python.<\/p>\n<h3>Brief on DataFrames<\/h3>\n<p>A DataFrame in pandas is a 2-dimensional data structure that holds data in a tabular sense. This means it lets us work in a context of rows and columns.<\/p>\n<p><strong>A dataframe is:<\/strong><\/p>\n<ul>\n<li>Mutable.<\/li>\n<li>Capable of holding columns of different types.<\/li>\n<li>Capable of performing arithmetic operations on rows and columns.<\/li>\n<li>A holder of labeled axes for the rows and columns.<\/li>\n<\/ul>\n<p><strong>Disadvantages of dataframes:<\/strong><\/p>\n<ul>\n<li>If a large file is stored in the computer&#8217;s RAM, there are chances that your system might crash.<\/li>\n<li>As only one part of the CPU is used, it can slow down while managing multiple rows.<\/li>\n<li>As there are many commands, it becomes difficult to remember them.<\/li>\n<li>Since libraries like Pandas are big, if they are installed in a small app, it can make it work slower.<\/li>\n<\/ul>\n<p>This is the DataFrame constructor we have-<\/p>\n<p>pandas.DataFrame (data, index, columns, dtype, copy)<br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/python-data-file-formats\/\">Read about Python Data File Formats \u2013 How to Read CSV, JSON, and XLS Files<\/a><\/strong><\/p>\n<h3 class=\"western\">Python Data Wrangling &#8211; Prerequisites<\/h3>\n<h4 class=\"western\">a. Python pandas<\/h4>\n<p>For aggregation and Data wrangling with Python, you will need the <i>pandas<\/i> library. It helps us with data manipulation and analysis. It has data structures and allows operations that we can use to manipulate numerical tables and time series.<\/p>\n<div id=\"attachment_21135\" style=\"width: 828px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pandas-2.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21135\" class=\"wp-image-21135 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pandas-2.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"818\" height=\"163\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pandas-2.png 818w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pandas-2-150x30.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pandas-2-300x60.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pandas-2-768x153.png 768w\" sizes=\"auto, (max-width: 818px) 100vw, 818px\" \/><\/a><p id=\"caption-attachment-21135\" class=\"wp-caption-text\">Aggregation and Data Wrangling with Python<\/p><\/div>\n<p>You can install it using the following command-<\/p>\n<p><strong>C:\\Users\\lifei&gt;pip install pandas<\/strong><\/p>\n<p>At the time of writing, we use version 0.23.1 of <i>pandas<\/i>.<\/p>\n<h4 class=\"western\">b. Python NumPy<\/h4>\n<p>NumPy is another<strong><a href=\"https:\/\/data-flair.training\/blogs\/python-library\/\" target=\"_blank\" rel=\"noopener\"> Python library<\/a><\/strong> that lets us handle large, multi-dimensional arrays and matrices. It also offers various high-level mathematical functions to help us deal with these.<\/p>\n<div id=\"attachment_21136\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/numpy.jpeg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21136\" class=\"wp-image-21136 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/numpy.jpeg\" alt=\"Aggregation and Data Wrangling with Python\" width=\"400\" height=\"225\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/numpy.jpeg 400w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/numpy-150x84.jpeg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/numpy-300x169.jpeg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><p id=\"caption-attachment-21136\" class=\"wp-caption-text\">Aggregation and Data Wrangling with Python<\/p><\/div>\n<p>To install this, you can try the following command in the command prompt-<\/p>\n<p><strong>C:\\Users\\lifei&gt;pip install numpy<\/strong><\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/nosql-database-in-python\/\" target=\"_blank\" rel=\"noopener\">How to Work with NoSQL Database in Python using PyMongo<\/a><\/strong><\/p>\n<h4>c. Python DataFrames<\/h4>\n<p>For our purpose, we will need two dataframes. We can create a DataFrame using any <a href=\"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/\"><strong>data type<\/strong><\/a>, like a list, a dictionary, or a series. We can also use NumPy and an array or another DataFrame to create it. Let\u2019s use a <a href=\"https:\/\/data-flair.training\/blogs\/python-dictionaries\/\"><strong>dictionary<\/strong> <\/a>for now.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; one=pandas.DataFrame({\r\n\t'emp_id':[1,2,3,4,5],\r\n\t'dept_name':['duvet','bidet','footwear','clothing','electronics'],\r\n\t'aisle':[1,2,3,4,5]})\r\n&gt;&gt;&gt; first=pandas.DataFrame(one)\r\n&gt;&gt;&gt; two=pandas.DataFrame({\r\n\t'emp_id':[6,7,8,9,10],\r\n\t'dept_name':['grocery','toys','laundry','frozen','stationery'],\r\n\t'aisle':[6,2,2,9,10]})\r\n&gt;&gt;&gt; second=pandas.DataFrame(two)<\/pre>\n<p>We also create a CSV file with UTF-8 format in Excel-<\/p>\n<div id=\"attachment_21137\" style=\"width: 345px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/csv-2.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21137\" class=\"wp-image-21137 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/csv-2.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"335\" height=\"216\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/csv-2.png 335w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/csv-2-150x97.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/csv-2-300x193.png 300w\" sizes=\"auto, (max-width: 335px) 100vw, 335px\" \/><\/a><p id=\"caption-attachment-21137\" class=\"wp-caption-text\">Aggregation and Data Wrangling with Python<\/p><\/div>\n<p>We save this as supermarket.csv.<\/p>\n<h3 class=\"western\">Why We Need Data Wrangling with Python<\/h3>\n<p>Much data obtained from various sources is raw and unusable. This could be messy or incomplete. With data wrangling with Python, we can perform operations on raw data to clean it in an organized format. Wrangling is essential to data science as a clean dataset leads to a more reliable model. Let\u2019s take a quick look at it.<\/p>\n<h4 class=\"western\">Dropping Missing Values<\/h4>\n<p>As you can see, we have no aisle number for the <i>frozen<\/i> department in our csv file.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os\r\n&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei\\\\Desktop')\r\n&gt;&gt;&gt; three=pandas.read_csv('supermarket.csv')\r\n&gt;&gt;&gt; pandas.isnull(three).any()\r\nemp_id        False\r\ndept_name     False\r\naisle         True\r\ndtype:        bool<\/pre>\n<p>This shows us that the \u2018aisle\u2019 column does have a missing value. The following command shows us the null values in the file-<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; three.head()<\/pre>\n<div id=\"attachment_21138\" style=\"width: 369px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/head.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21138\" class=\"wp-image-21138 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/head.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"359\" height=\"171\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/head.png 359w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/head-150x71.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/head-300x143.png 300w\" sizes=\"auto, (max-width: 359px) 100vw, 359px\" \/><\/a><p id=\"caption-attachment-21138\" class=\"wp-caption-text\">Aggregation and Data Wrangling with Python<\/p><\/div>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-file\/\" target=\"_blank\" rel=\"noopener\">Let&#8217;s Revise Python File I\/O \u2013 Python Write to File and Read File<\/a> <\/strong><br \/>\nTo drop this record, we use the following command-<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; three.dropna()<\/pre>\n<div id=\"attachment_21139\" style=\"width: 370px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/dropna.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21139\" class=\"wp-image-21139 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/dropna.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"360\" height=\"147\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/dropna.png 360w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/dropna-150x61.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/dropna-300x123.png 300w\" sizes=\"auto, (max-width: 360px) 100vw, 360px\" \/><\/a><p id=\"caption-attachment-21139\" class=\"wp-caption-text\">Data Wrangling with Python &#8211;\u00a0Dropping Missing Values<\/p><\/div>\n<h4>Grouping Data<\/h4>\n<p>The pandas gorupby() method returns a DataFrameGroupBy object. Calling the method value_counts() returns the number of occurrences for each unique value in the column we specify.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; three.groupby('aisle').dept_name.value_counts()<\/pre>\n<div id=\"attachment_21140\" style=\"width: 637px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/groupby.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21140\" class=\"wp-image-21140 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/groupby.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"627\" height=\"174\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/groupby.png 627w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/groupby-150x42.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/groupby-300x83.png 300w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/a><p id=\"caption-attachment-21140\" class=\"wp-caption-text\">Data Wrangling with Python &#8211; Grouping Data<\/p><\/div>\n<p>This tells us about the correlation between aisle number and department name. This lets us discover trends in data. In situations where we want to see trends by the city, we can group records by their geographical factors.<\/p>\n<h4 class=\"western\">a. Finding unique values<\/h4>\n<p>For this, we can use the unique() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; two.aisle.unique()<\/pre>\n<p><strong>array([ 6, 2, 9, 10], dtype=int64)<\/strong><\/p>\n<h4 class=\"western\">b. Length of dataframe<\/h4>\n<p>Then the len() function gives us the length of the dataframe.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; len(two)<\/pre>\n<p>5<br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/python-for-data-science\/\" target=\"_blank\" rel=\"noopener\">Do you know how I Used These 5 Steps to learn Python for Data Science<\/a><\/strong><\/p>\n<h4 class=\"western\">Filtering Data<\/h4>\n<p>To gain a subset of data using some criterion, we can filter out parts we don&#8217;t need.<\/p>\n<div id=\"attachment_21173\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Filtering-Data-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21173\" class=\"wp-image-21173 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Filtering-Data-01.jpg\" alt=\"Aggregation and Data Wrangling with Python\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Filtering-Data-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Filtering-Data-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Filtering-Data-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Filtering-Data-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Filtering-Data-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-21173\" class=\"wp-caption-text\">Data Wrangling with Python &#8211; Filtering Data<\/p><\/div>\n<h4 class=\"western\">a. Single Conditions<\/h4>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; two[two['aisle']&gt;3]<\/pre>\n<div id=\"attachment_21141\" style=\"width: 365px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21141\" class=\"wp-image-21141 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter1.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"355\" height=\"121\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter1.png 355w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter1-150x51.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter1-300x102.png 300w\" sizes=\"auto, (max-width: 355px) 100vw, 355px\" \/><\/a><p id=\"caption-attachment-21141\" class=\"wp-caption-text\">Filtering Data &#8211;\u00a0Single Conditions<\/p><\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; two[two['aisle']==2]<\/pre>\n<div id=\"attachment_21142\" style=\"width: 338px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter2.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21142\" class=\"wp-image-21142 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter2.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"328\" height=\"97\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter2.png 328w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter2-150x44.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter2-300x89.png 300w\" sizes=\"auto, (max-width: 328px) 100vw, 328px\" \/><\/a><p id=\"caption-attachment-21142\" class=\"wp-caption-text\">Filtering Data &#8211;\u00a0Single Conditions<\/p><\/div>\n<h4 class=\"western\">b. Multiple Conditions<\/h4>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; two[(two['aisle']==2) &amp; (two['dept_name']=='toys')]<\/pre>\n<div id=\"attachment_21143\" style=\"width: 682px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter3.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21143\" class=\"wp-image-21143 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter3.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"672\" height=\"76\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter3.png 672w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter3-150x17.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter3-300x34.png 300w\" sizes=\"auto, (max-width: 672px) 100vw, 672px\" \/><\/a><p id=\"caption-attachment-21143\" class=\"wp-caption-text\">Filtering Data &#8211; Multiple Conditions<\/p><\/div>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-counter\/\" target=\"_blank\" rel=\"noopener\">Let&#8217;s Read about Python Counter \u2013 Python Collections Type<\/a><\/strong><\/p>\n<h4>c. Describing your Data<\/h4>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; (two[(two['aisle']==2) &amp; (two['dept_name']=='toys')]).describe()<\/pre>\n<div id=\"attachment_21144\" style=\"width: 269px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter4.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21144\" class=\"wp-image-21144 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter4.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"259\" height=\"233\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter4.png 259w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter4-150x135.png 150w\" sizes=\"auto, (max-width: 259px) 100vw, 259px\" \/><\/a><p id=\"caption-attachment-21144\" class=\"wp-caption-text\">Filtering Data &#8211; Describing your Data<\/p><\/div>\n<h4>d. Filtering through Values<\/h4>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; two[two.aisle.isin([2,7,9])]<\/pre>\n<div id=\"attachment_21145\" style=\"width: 407px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter5.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21145\" class=\"wp-image-21145 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter5.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"397\" height=\"122\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter5.png 397w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter5-150x46.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/filter5-300x92.png 300w\" sizes=\"auto, (max-width: 397px) 100vw, 397px\" \/><\/a><p id=\"caption-attachment-21145\" class=\"wp-caption-text\">Filtering through Values<\/p><\/div>\n<h3>Pivoting Dataset<\/h3>\n<p>Pivoting lets us reshape a DataFrame by column\/index values. In other words, it produces a pivot table.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; (two.pivot(index='emp_id',columns='aisle',values='dept_name')).tail()<\/pre>\n<div id=\"attachment_21147\" style=\"width: 595px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pivot.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21147\" class=\"wp-image-21147 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pivot.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"585\" height=\"244\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pivot.png 585w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pivot-150x63.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pivot-300x125.png 300w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><\/a><p id=\"caption-attachment-21147\" class=\"wp-caption-text\">Data Wrangling with Python &#8211;\u00a0\u00a0Pivoting a Dataset<\/p><\/div>\n<p>Providing a number to this tail() call will give us that many records.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; (two.pivot(index='emp_id',columns='aisle',values='dept_name')).tail(3)<\/pre>\n<div id=\"attachment_21148\" style=\"width: 534px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pivot2.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21148\" class=\"wp-image-21148 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pivot2.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"524\" height=\"171\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pivot2.png 524w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pivot2-150x49.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/pivot2-300x98.png 300w\" sizes=\"auto, (max-width: 524px) 100vw, 524px\" \/><\/a><p id=\"caption-attachment-21148\" class=\"wp-caption-text\">Data Wrangling with Python &#8211;\u00a0\u00a0Pivoting a Dataset<\/p><\/div>\n<p><strong>a. Shifting a pivoted dataset<\/strong><\/p>\n<p>We can shift the DataFrame\u2019s index by a certain number of periods using the shift() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; (two.pivot(index='emp_id',columns='aisle',values='dept_name')).tail(3).shift(1)<\/pre>\n<div id=\"attachment_21152\" style=\"width: 449px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/shift.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21152\" class=\"wp-image-21152 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/shift.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"439\" height=\"187\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/shift.png 439w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/shift-150x64.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/shift-300x128.png 300w\" sizes=\"auto, (max-width: 439px) 100vw, 439px\" \/><\/a><p id=\"caption-attachment-21152\" class=\"wp-caption-text\">Data Wrangling with Python &#8211;\u00a0\u00a0Shifting a pivoted dataset<\/p><\/div>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-operator-overloading\/\" target=\"_blank\" rel=\"noopener\">Let&#8217;s Discuss Python Operator Overloading and Python Magic Methods<\/a><\/strong><\/p>\n<h4 class=\"western\">Melting Shifted Datasets<\/h4>\n<p>We can also melt a dataset (unpivot it).<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; (two.melt(id_vars=['emp_id'],value_name='dept_name')).tail()<\/pre>\n<div id=\"attachment_21153\" style=\"width: 694px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/melt.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21153\" class=\"wp-image-21153 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/melt.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"684\" height=\"225\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/melt.png 684w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/melt-150x49.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/melt-300x99.png 300w\" sizes=\"auto, (max-width: 684px) 100vw, 684px\" \/><\/a><p id=\"caption-attachment-21153\" class=\"wp-caption-text\">Data Wrangling with Python &#8211; Melting Shifted Datasets<\/p><\/div>\n<h4>Merging Melted Data<\/h4>\n<p>We can join melted DataFrames into one Analytical Base Table using the merge() function.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; melt1=two.melt(id_vars=['emp_id'],value_name='dept_name')\r\n&gt;&gt;&gt; melt2=two.melt(id_vars=['emp_id'],value_name='aisle')\r\n&gt;&gt;&gt; pandas.merge(melt1,melt2,on=['emp_id']).tail(1)<\/pre>\n<div id=\"attachment_21154\" style=\"width: 700px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/merge.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21154\" class=\"wp-image-21154 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/merge.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"690\" height=\"74\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/merge.png 690w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/merge-150x16.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/merge-300x32.png 300w\" sizes=\"auto, (max-width: 690px) 100vw, 690px\" \/><\/a><p id=\"caption-attachment-21154\" class=\"wp-caption-text\">Aggregation and Data Wrangling with Python<\/p><\/div>\n<h4 class=\"western\">Reducing to an ABT<\/h4>\n<p>Finally, to reduce this result into an analytical base table, we do the following:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from functools import reduce\r\n&gt;&gt;&gt; base=two[['aisle','dept_name','emp_id']]\r\n&gt;&gt;&gt; feature=[base]+[melt1,melt2]\r\n&gt;&gt;&gt; abt=reduce(lambda left,right: pandas.merge(left,right,on=['emp_id']),[melt1,melt2])\r\n&gt;&gt;&gt; abt.tail(1)<\/pre>\n<div id=\"attachment_21155\" style=\"width: 700px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/abt.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21155\" class=\"wp-image-21155 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/abt.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"690\" height=\"75\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/abt.png 690w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/abt-150x16.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/abt-300x33.png 300w\" sizes=\"auto, (max-width: 690px) 100vw, 690px\" \/><\/a><p id=\"caption-attachment-21155\" class=\"wp-caption-text\">Data Wrangling with Python &#8211;\u00a0Reducing into an ABT<\/p><\/div>\n<h4 class=\"western\">Concatenating Data<\/h4>\n<p>We can concatenate datasets along an axis using the concat() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; pandas.concat([one,two])<\/pre>\n<div id=\"attachment_21157\" style=\"width: 378px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/concat.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21157\" class=\"wp-image-21157 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/concat.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"368\" height=\"289\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/concat.png 368w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/concat-150x118.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/concat-300x236.png 300w\" sizes=\"auto, (max-width: 368px) 100vw, 368px\" \/><\/a><p id=\"caption-attachment-21157\" class=\"wp-caption-text\">Concatenating Data<\/p><\/div>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-sequences-and-collections\/\" target=\"_blank\" rel=\"noopener\">Have a look &#8211; Lessons on Python Sequences and Collections<\/a><\/strong><\/p>\n<h4 class=\"western\">Exporting Data<\/h4>\n<p>Finally, we can export our clean and filtered data to an Excel sheet.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; res=pandas.concat([one,two])\r\n&gt;&gt;&gt; res.to_excel('res.xlsx')<\/pre>\n<h3 class=\"western\">How does Python Aggregate Data?<\/h3>\n<p>To apply aggregations on our datasets, we can use the Python numpy library along with pandas. It allows for summarizing data based on categories.<\/p>\n<div id=\"attachment_21185\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Python-Aggregate-Data-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21185\" class=\"wp-image-21185 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Python-Aggregate-Data-01.jpg\" alt=\"Learn Aggregation and Data Wrangling with Python\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Python-Aggregate-Data-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Python-Aggregate-Data-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Python-Aggregate-Data-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Python-Aggregate-Data-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Python-Aggregate-Data-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-21185\" class=\"wp-caption-text\">Learn Aggregation and Data Wrangling with Python<\/p><\/div>\n<p>Let\u2019s create a dataframe first.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; df=pandas.DataFrame(numpy.random.randn(7,3),\r\n\tindex=pandas.date_range('1\/1\/2000',periods=7),\r\n\tcolumns=['A','B','C'])\r\n&gt;&gt;&gt; print(df)<\/pre>\n<div id=\"attachment_21158\" style=\"width: 511px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregation-dataframe.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21158\" class=\"wp-image-21158 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregation-dataframe.png\" alt=\"How Python Aggregate Data\" width=\"501\" height=\"230\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregation-dataframe.png 501w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregation-dataframe-150x69.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregation-dataframe-300x138.png 300w\" sizes=\"auto, (max-width: 501px) 100vw, 501px\" \/><\/a><p id=\"caption-attachment-21158\" class=\"wp-caption-text\">How Python Aggregate Data<\/p><\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; print(df.rolling(window=3,min_periods=1))\r\nRolling [window=3,min_periods=1,center=False,axis=0]<\/pre>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-library\/\" target=\"_blank\" rel=\"noopener\">Let&#8217;s explore The Best Article on Python\u2019s Various Libraries<\/a><\/strong><\/p>\n<h4 class=\"western\">a. Python Aggregating an entire dataframe<\/h4>\n<p>We can aggregate an entire dataframe as-<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; r=df.rolling(window=3,min_periods=1)\r\n&gt;&gt;&gt; r.aggregate(numpy.sum)<\/pre>\n<div id=\"attachment_21159\" style=\"width: 506px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-whole.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21159\" class=\"wp-image-21159 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-whole.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"496\" height=\"223\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-whole.png 496w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-whole-150x67.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-whole-300x135.png 300w\" sizes=\"auto, (max-width: 496px) 100vw, 496px\" \/><\/a><p id=\"caption-attachment-21159\" class=\"wp-caption-text\">How Python Aggregates Data<\/p><\/div>\n<p>As you can see, this keeps aggregating the previous value into itself. Another aggregation we can use is max-<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; r.aggregate(numpy.max)<\/pre>\n<div id=\"attachment_21160\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-max.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21160\" class=\"wp-image-21160 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-max.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"500\" height=\"222\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-max.png 500w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-max-150x67.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-max-300x133.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><p id=\"caption-attachment-21160\" class=\"wp-caption-text\">Python Aggregating an entire dataframe<\/p><\/div>\n<p>Other aggregations include <i>mean<\/i>, <i>median<\/i>, and <i>min<\/i>.<\/p>\n<h4 class=\"western\">b. Python Aggregating one column<\/h4>\n<p>To aggregate just one column, we can use its index.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; r['B'].aggregate(numpy.mean)<\/pre>\n<div id=\"attachment_21163\" style=\"width: 416px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-one.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21163\" class=\"wp-image-21163 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-one.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"406\" height=\"220\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-one.png 406w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-one-150x81.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-one-300x163.png 300w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/a><p id=\"caption-attachment-21163\" class=\"wp-caption-text\">Python Aggregating one column<\/p><\/div>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-property-problem-solution\/\" target=\"_blank\" rel=\"noopener\">Let&#8217;s See the Python Property \u2013 The Problem and Solution<\/a><\/strong><\/p>\n<h4>c. Python Aggregating multiple columns<\/h4>\n<p>We can also specify more than one column to aggregate.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; r[['B','C']].aggregate(numpy.sum)<\/pre>\n<div id=\"attachment_21164\" style=\"width: 465px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-multiple.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21164\" class=\"wp-image-21164 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-multiple.png\" alt=\"Aggregation and Data Wrangling with Python\" width=\"455\" height=\"215\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-multiple.png 455w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-multiple-150x71.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/aggregate-multiple-300x142.png 300w\" sizes=\"auto, (max-width: 455px) 100vw, 455px\" \/><\/a><p id=\"caption-attachment-21164\" class=\"wp-caption-text\">Python Aggregating multiple columns<\/p><\/div>\n<p>So, this was all about\u00a0Aggregation and Data Wrangling with Python. Hope you like our explanation.<\/p>\n<h3>Conclusion<\/h3>\n<p>Hence, this was how to clean your data with wrangling and then how to apply aggregations on it. Did we miss out on something in Aggregation and Data Wrangling with Python? Drop your suggestions in the comments.<br \/>\nSee Also-\u00a0<strong><a href=\"https:\/\/data-flair.training\/blogs\/python-os-module\/\" target=\"_blank\" rel=\"noopener\">List of 63 Python os Modules<\/a><\/strong><br \/>\n<strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Data_wrangling\">For reference<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our previous tutorial, we talked about Python Django. Today, in this Data Wrangling tutorial, we will see Python Aggregation and Data Wrangling with the Python Programming Language. Moreover, we will discuss prerequisites\u00a0 &amp;\u00a0reasons&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":21167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[3397,3399,3402,3510,3511,3512,10466,10727,10755,16195],"class_list":["post-21110","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-data-preparation-in-python","tag-data-preprocessing-code-in-python","tag-data-preprocessing-using-python","tag-data-wrangling-examples","tag-data-wrangling-tools","tag-data-wrangling-with-python-pdf","tag-python-dataframes","tag-python-numpy","tag-python-pandas","tag-why-we-need-data-wrangling-with-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Learn Aggregation and Data Wrangling with Python - DataFlair<\/title>\n<meta name=\"description\" content=\"What is Data Wrangling with Python, Python Aggregate, Dropping Missing Values, Grouping Data, Filtering Data, Python Aggregate\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Learn Aggregation and Data Wrangling with Python - DataFlair\" \/>\n<meta property=\"og:description\" content=\"What is Data Wrangling with Python, Python Aggregate, Dropping Missing Values, Grouping Data, Filtering Data, Python Aggregate\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-14T04:10:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-28T09:42:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Aggregation-and-Data-Wrangling-with-Python-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Learn Aggregation and Data Wrangling with Python - DataFlair","description":"What is Data Wrangling with Python, Python Aggregate, Dropping Missing Values, Grouping Data, Filtering Data, Python Aggregate","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/","og_locale":"en_US","og_type":"article","og_title":"Learn Aggregation and Data Wrangling with Python - DataFlair","og_description":"What is Data Wrangling with Python, Python Aggregate, Dropping Missing Values, Grouping Data, Filtering Data, Python Aggregate","og_url":"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-07-14T04:10:54+00:00","article_modified_time":"2026-04-28T09:42:19+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Aggregation-and-Data-Wrangling-with-Python-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Learn Aggregation and Data Wrangling with Python","datePublished":"2018-07-14T04:10:54+00:00","dateModified":"2026-04-28T09:42:19+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/"},"wordCount":1390,"commentCount":2,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Aggregation-and-Data-Wrangling-with-Python-01.jpg","keywords":["data preparation in python","data preprocessing code in python","data preprocessing using python","data wrangling examples","data wrangling tools","data wrangling with python pdf","Python DataFrames","Python NumPy","Python Pandas","Why we need Data Wrangling with Python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/","url":"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/","name":"Learn Aggregation and Data Wrangling with Python - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Aggregation-and-Data-Wrangling-with-Python-01.jpg","datePublished":"2018-07-14T04:10:54+00:00","dateModified":"2026-04-28T09:42:19+00:00","description":"What is Data Wrangling with Python, Python Aggregate, Dropping Missing Values, Grouping Data, Filtering Data, Python Aggregate","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Aggregation-and-Data-Wrangling-with-Python-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Aggregation-and-Data-Wrangling-with-Python-01.jpg","width":1200,"height":628,"caption":"Aggregation and Data Wrangling with Python"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/data-wrangling-with-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Learn Aggregation and Data Wrangling with Python"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/21110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=21110"}],"version-history":[{"count":9,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/21110\/revisions"}],"predecessor-version":[{"id":148023,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/21110\/revisions\/148023"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/21167"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=21110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=21110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=21110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}