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

Python: Pandas Lessons



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

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