Archive

Archive for July 22, 2019

Python: Pandas Lesson 3

July 22, 2019 13 comments


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

file_name=’data_file_zoo.csv’

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.

df.sort_values(‘animal’,inplace=True)
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..



Advertisements