Python Pandas – Tutorial (Part N ° 2)

Share this post

This article is the thenext part of the first part of my tutorial on the Python Pandas library . The idea here is to go further in the discovery of this library and in particular to give the keys to the set manipulations it offers. To put it simply, you have DataFrame but you will have to combine them together in order to work on your characteristics. In short, you will want to do “SQL like” on your data! Good news, this bookstore allows you to do it in the most elegant and simple way.

Let’s prepare our dataset

For this tutorial we will create two matrices A and B:

import pandas as pd
A = pd.DataFrame({'Col1': [1, 2, 3],
                  'Col2': [4, 4, 6]},
                 index=['rowA1', 'rowA2', 'rowA3'])
print("Matrice A\n", A)
B = pd.DataFrame({'Col1': [1, 3],
                  'Col3': [7, 8],
                  'Col4': [9, 10]},
                 index=['rowB1', 'rowB2'])
print("\nMatrice B\n", B)

Here they are :

Matrice A
        Col1  Col2
rowA1     1     4
rowA2     2     4
rowA3     3     6

Matrice B
        Col1  Col3  Col4
rowB1     1     7     9
rowB2     3     8    10

Count the values ​​of a column

The value_counts () method appended to a column of a DataFrame is used to list the values ​​of this column with their number of occurrences:

A["Col1"].value_counts()
3    1
2    1
1    1
Name: Col1, dtype: int64

Browse the rows of a DataFrame

Sometimes it can be useful to step through a DataFrame line by line. for this Pandas offers two iterrows () and itertuples () iterators . Both of these methods have their subtlety as itertuples()which is supposed to be faster than iterrows(), or iterrows() might not match from row to row. In short, I advise you to take a good look at the Pandas documentation before embarking on this path.

To iterate therefore, you can therefore use:

for index, ligne in monDataFrame.iterrows():
    print (ligne["Colonne X"])

Filter the rows of a DataFrame

Filtering is an essential action when working with datasets. For this we have two methods, one Python and the other via Pandas.

B.loc[B['Col3'] == 7]

Or just

B[B['Col3'] == 7]

In both cases we filter the DataFrame on the column ‘Col3’ which has the value 7.

We can of course combine several conditions but be careful not to use the boolean operators and and or in this case but & and | in place:

B.loc[(B['Col3'] == 7) | (B['Col3'] > 1)]['Col1']
B.loc[(B['Col3'] == 7) & (B['Col3'] > 1)]['Col1']

Now we can use the Filter method of the DataFrame Pandas. It does not provide exactly the same services but can be very practical to like for example:

A.filter(like='A3', axis=0)

Joins

To do this, nothing could be simpler, use the merge method and specify the join mode:

  • INNER : strict join
  • LEFT : left joint
  • RIGHT : right joint
  • OUTER : full outer
pd.merge(A, B, how='inner', on='Col1')

Union

To do this, nothing could be simpler, use the concat method:

pd.concat([A, B])

Group By

The groupby method is used to group rows on a criterion and to perform operations on the columns. This is exactly what SQL Group By does.

print ("Group By / Count:\n ", A.groupby("Col2")['Col1'].size(), "\n")
print ("Group By / Moyenne:\n ", A.groupby("Col2")['Col1'].mean(), "\n")

It is possible to specify several transformations on the same group:

# Multiple opérations
import numpy as np
A.groupby('Col2').agg({'Col1': np.mean, 'Col2': np.size})

Download or check out the Jupyter notebook with the above examples on GitHub .

Share this post

Benoit Cayla

In more than 15 years, I have built-up a solid experience around various integration projects (data & applications). I have, indeed, worked in nine different companies and successively adopted the vision of the service provider, the customer and the software editor. This experience, which made me almost omniscient in my field naturally led me to be involved in large-scale projects around the digitalization of business processes, mainly in such sectors like insurance and finance. Really passionate about AI (Machine Learning, NLP and Deep Learning), I joined Blue Prism in 2019 as a pre-sales solution consultant, where I can combine my subject matter skills with automation to help my customers to automate complex business processes in a more efficient way. In parallel with my professional activity, I run a blog aimed at showing how to understand and analyze data as simply as possible: datacorner.fr Learning, convincing by the arguments and passing on my knowledge could be my caracteristic triptych.

View all posts by Benoit Cayla →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Privacy Preference Center

Analytics

NOTICE RELATING TO COOKIES
What is a cookie and what is it used for?

A cookie (or connection witness) is a text file that can be saved, subject to your choices, in a dedicated space on the hard drive of your terminal (computer, tablet, etc.) when consulting a online service through your browser software.
It is transmitted by a website's server to your browser. Each cookie is assigned an anonymous identifier. The cookie file allows its issuer to identify the terminal in which it is registered during the period of validity or registration of the cookie concerned. A cookie cannot be traced back to a natural person.

When you visit this site, it may be required to install, subject to your choice, various statistical cookies.
What types of cookies are placed by the website?


Google Analytics & Matomo Statistics Cookies

These cookies are used to establish statistics of visits to my site and to detect navigation problems in order to monitor and improve the quality of our services.
Exercise your choices according to the browser you use

You can configure your browser at any time in order to express and modify your wishes in terms of cookies, and in particular regarding statistical cookies. You can express your choices by setting your browser to refuse certain cookies.

If you refuse cookies, your visit to the site will no longer be counted in Google Analytics & Matomo and you will no longer be able to benefit from a number of features that are nevertheless necessary to navigate certain pages of this site.
However, you can oppose the registration of cookies by following the operating procedure available below:

On Internet Explorer
1. Go to Tools> Internet Options.
2. Click on the privacy tab.
3. Click on the advanced button, check the box "Ignore automatic management of cookies".

On Firefox
1. At the top of the Firefox window, click the Firefox button (Tools menu in Windows XP), then select Options.
2. Select the Privacy panel.
3. Configure Conservation rules: to use the personalized parameters for the history.
4. Uncheck Accept cookies.

On Chrome
1. Click on the wrench icon which is located in the browser toolbar.
2. Select Settings.
3. Click Show advanced settings.
4. In the “Confidentiality” section, click on the Content settings button.
5. In the "Cookies" section, you can block cookies and data from third-party sites

On Safari
1. Go to Settings> Preferences
2. Click on the Privacy tab
3. In the "Block cookies" area, check the "always" box.

About Opera
1. Go to Settings> Preferences
2. Click on the advanced tab
3. In the "Cookies" area, check the "Never accept cookies" box.
social network sharing cookies

On certain pages of this site there are buttons or modules of third-party social networks that allow you to use the functionalities of these networks and in particular to share content on this site with other people.
When you go to a web page on which one of these buttons or modules is located, your browser can send information to the social network which can then associate this visualization with your profile.

Social network cookies, over which this site has no control, may then be placed in your browser by these networks. I invite you to consult the confidentiality policies specific to each of these social networking sites, in order to become aware of the purposes for using the browsing information that social networks can collect using these buttons and modules.
- Twitter
- Google+
- LinkedIn

Statistiqcs only

Fork me on GitHub