Archive

Archive for July 17, 2019

Python: Pandas Lessons

July 17, 2019 13 comments


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..

———————————
Update on: 29/7/2019



:: Pandas Lessons Post ::

Lesson 1 Lesson 2 Lesson 3 Lesson 4
Lesson 5



Follow me on Twitter..



Advertisements

Python: Triangle, Pentagonal, and Hexagonal 



Python: Triangle, Pentagonal, and Hexagonal 
Problem No.45 @ Projecteuler
Completed on: Thu, 11 Jul 2019, 21:31

Another straight-forward problem, in this task I create three functions each for Triangle, Pentagonal, and Hexagonal and we return the value of the formulas as been stated in the problem.

Using a for loop and a number range, I store the results in a list tn, pn, hn. then comparing the values in the three lists searching for same value.


The Code:


# P45
# Solved
# Completed on Thu, 11 Jul 2019, 21:31


def tn (n) :

return int(n*(n+1)/2)

def pn(n):

return int(n*(3*n-1)/2)

def hn (n):

return int(n*(2*n-1))

tn_list =[]
pn_list=[]
hn_list=[]

n = 0

# Notes: I run the code for large range, but to save more time after 5000 i select +10,000 each time.

for n in range (5000,60000):

tn_list.append(tn(n))

pn_list.append(pn(n))

hn_list.append(hn(n))

print ([x for x in tn_list if x in pn_list and x in hn_list])





Follow me on Twitter..