Archive

Posts Tagged ‘Learning’

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

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
Advertisements

Python: Pandas Lesson

July 21, 2019 1 comment

Learning : DataFrame and some commands
Subject: Pandas printing selected rows

First thing we will do today, we will add another coloumn to our CSV data_file_zoo.csv, we will add ‘years’ this will be hwo old each animal in the zoo is.

File_Name: data_file_zoo.csv
animal,id,water_need,supervisor,cage_no,years
elephant,1001,500,Peter,5,5
elephant,1002,600,John,5,4
elephant,1003,550,Peter,5,4
tiger,1004,300,mark,4,8
tiger,1005,320,mark,4,9
tiger,1006,330,peter,3,5
tiger,1007,290,mark,3,3
tiger,1008,310,D.J,4,4
zebra,1009,200,D.J,8,
zebra,1010,220,D.J,9,8
zebra,1011,240,D.J,9,7
zebra,1012,230,mark,8,6
zebra,1013,220,D.J,8,3
zebra,1014,100,D.J,9,4
zebra,1015,80,peter,9,4
lion,1016,420,,1,9
lion,1017,600,D.J,1,8
lion,1018,500,,2,4
lion,1019,390,,2,5
kangaroo,1020,410,peter,7,8
kangaroo,1021,430,D.J,7,6
kangaroo,1022,410,mark,7,1


As we just update out file, we need to load it to the memory by calling the df (dataframe), this will happen once we run our code.
Here is a screen shot of the new data using print(df)



Lets say we want to know how many animals are numder 6 years. Here we will use df.loc to locate what we are looking for.

age_less_6 = df.loc[(dfyears<6)]
# To print we may use this:
print(‘ we have {} animals less than 6 years’.format(len(age_less_6)))

Now, we want to print only lion rows:
lino_rows = df.loc[(df.animal==’lion’)]



Here is only rows with animal name ‘elephants’:
elephant_rows=df.loc[(df.animal==’elephant’)]


Now let’s print only the rows with lion and elephants:
lion_and_elephant = df.loc[(df.animal==’lion’) | (df.animal == ‘elephant’)]


What if we want all the data but not the rows with lino or elephant.
all_exclude_lion_elephant=df.loc[(df.animal !=’lion’) & (df.animal !=’elephant’)]

Follow me on Twitter..

Python: Pandas Lessons

July 17, 2019 1 comment


Learning : DataFrame and some commands
Subject:

This is my first hours in Pandas, until now thing are going smooth. I am using pythonanywhere on my PC, and jupyterlab on my galaxy tab S4.

In this post and coming once under name Pandas Lesson I will write some commands and what-ever I think I may need.

So, first thing we need a csv file with data to play with, so I search for some thing simple, i found one with zoo data!, I add two new column to it. so lets see it.

File_Name: data_file_zoo.csv
animal,id,water_need,supervisor,cage_no
elephant,1001,500,Peter,5
elephant,1002,600,John,5
elephant,1003,550,Peter,5
tiger,1004,300,mark,4
tiger,1005,320,mark,4
tiger,1006,330,peter,3
tiger,1007,290,mark,3
tiger,1008,310,D.J,4
zebra,1009,200,D.J,8
zebra,1010,220,D.J,9
zebra,1011,240,D.J,9
zebra,1012,230,mark,8
zebra,1013,220,D.J,8
zebra,1014,100,D.J,9
zebra,1015,80,peter,9
lion,1016,420,,1
lion,1017,600,D.J,1
lion,1018,500,,2
lion,1019,390,,2
kangaroo,1020,410,peter,7
kangaroo,1021,430,D.J,7
kangaroo,1022,410,mark,7

I add the ” supervisor and cage_no ” to the original file so we will have more room to manipulate.

First Command: first thing we need to call pandas library using import, and set the file name and dataframe.

import pandas as pd
file_name=’data_file_zoo.csv’
df=pd.read_csv(file_name, delimiter=’,’)

We will use this part for all our initialization part


Other Command: Here are other commands that works with dataframe df.

print(df) Will print out all the data from the file.
print (df.head()) Will print first 5 rows
print (df.tail()) Will print last 5 rows
print (df.sample(3)) Will print random 3 rows from the dataframe.
print(df.columns) Will print the columns in the file
print (df[[‘id’,’animal’,’cage_no’]]) Print only the data from column you want
print (df[[‘id’,’animal’,’cage_no’]].sample(3)) Print random 3 rows of only ‘id’,’animal’,’cage_no’ columns
print (df[df.animal==’lion’]) Get all the rows with animal name = lion . case sensitive
print(df.head()[[‘animal’,’id’]]) Print first five rows of only animal and id



Wrapped up: This is a step one, pandas has many to read about and to learn, I start this initiative just for my self, and i select the hard way to do this, this is not important to my current job, this is nothing that any body will ask me about, but i want to learn and I think i will go further in this self-taught learning sessions..

Python Project

November 23, 2018 Leave a comment

Python Project:

Two months back I start to learn #Python “Python is a #computer #programming #language “, you can say it grabbed me very quickly. I start to read some #sample #code from the net, then downloading the requirement and after 10 working days I was writing #codes in python and doing what I love Solving #mathematics puzzles with #python’.

So I decide to contribute in training or publishing python language by posting some codes block with explaining and comments, also by developing an application so we will learn on a real example.

I am publishing some codes on my Twitter (@h_ta3kees):

https://twitter.com/h_ta3kees?s=09

and I will do the same here on my blog.

The coming posts will be about the application that we will start develop I will call it ‘Expenditure App’.