Home > Learning, Lesson, Projects/Experiments, Python > Python: Pandas Lesson 3

Python: Pandas Lesson 3

Learning : Pandas Lesson 3
Subject: dataframe (sort, where and filters)

In my Last post Pandaas Lesson 2, we show some commands that will output part of our dataframe (df) such as if we want to output the information we have about lions, or other animals in the Zoo file. Or to see what aminals fell under particular supervisor. Also I try to add a print statment over each output table to show/describe the table content.

In theis Lesson, or let’s say in this post I will share another bunch of commands dealing with one table of data. We will keep using our Zoo data file. So first I wll call the dataframe df.

import padas and call df

import pandas as pd


df=pd.read_csv(file_name, delimiter=’,’)

print(‘\n Data from Zoo file..’,df)

So, if we want to sort the data based on supervisor name.

df.sort_values(‘supervisor’, inplace=True)
print(‘\n\n Sorted data with Supervisor Name\n’,df)

First thing to notes that we have two group of supervisors name ‘peter’ one with small ‘p’, another with Big ‘P’. Another thing to see that we have some ‘lions’ with NaN under supervisor, this meas there is no data in that feilds. I will not change this now, let’s do this in another lesson.

So, let’s sort the data now with anumal type.

print(‘\n\n Sort with animal type.\n’,df)

If we want to print all animal data under mark supervision, other data will be shown as NaN.
mark_supervision = df[‘supervisor’]==’mark’
df.where(mark_supervision, inplace = True)
print(‘\n\n Any rows else than Mark as supervisore will be as NaN\n’,df)

If we want to add another filter to the upper dataframe to show animals under mark supervision if the animal age is more than 7.
age_biger_7 = df[‘years’] >7
df.where(mark_suoervision & age_biger_7, inplace = True)
print(‘\n\n Only rows under mark supervision if animal age > 7 \n’,df)

:: Pandas Lessons Post ::

Lesson 1 Lesson 2 Lesson 3 Lesson 4
Lesson 5

Follow me on Twitter..

  1. August 13, 2019 at 3:36 pm

    Thank you for sharing awesome information to public this article is very much usefull for who are looking for the python. Here is another source where you will get more detailed information Python Certification Course in Bangalore

    • August 14, 2019 at 10:52 am

      Thanks for your visit and Comments

  1. July 23, 2019 at 8:34 am
  2. July 29, 2019 at 8:24 am
  3. July 29, 2019 at 8:28 am
  4. July 29, 2019 at 8:29 am
  5. July 31, 2019 at 8:46 am
  6. August 4, 2019 at 8:32 am
  7. August 6, 2019 at 5:30 am
  8. August 8, 2019 at 8:32 am
  9. August 11, 2019 at 8:35 am
  10. August 13, 2019 at 8:35 am
  11. August 15, 2019 at 8:33 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: