Archive
Python: Pandas Lesson
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’)]
:: Pandas Lessons Post ::
Lesson 1 | Lesson 2 | Lesson 3 | Lesson 4 |
Lesson 5 |