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 |
-
July 22, 2019 at 8:31 amPython: Pandas Lesson 3 | Ali's Photography Space...
-
July 23, 2019 at 8:33 amPython: Pandas Lesson 4 | Ali's Photography Space...
-
July 29, 2019 at 8:24 amPython: Pandas Lesson 5 | Ali's Photography Space...
-
July 29, 2019 at 8:29 amPython: Pandas Lessons | Ali's Photography Space...
-
July 31, 2019 at 8:46 amPython: Pandas more commands | Ali's Photography Space...
-
August 4, 2019 at 8:32 amPython: Pandas Lesson 6 | Ali's Photography Space...
-
August 6, 2019 at 8:30 amPython: Pandas Lesson 7 | Ali's Photography Space...
-
August 8, 2019 at 8:32 amPython: Pandas Lesson 8 | Ali's Photography Space...
-
August 11, 2019 at 8:35 amPython: Pandas Lesson 9 | Ali's Photography Space...
-
August 13, 2019 at 8:35 amPython: Pandas Lesson 10 | Ali's Photography Space...
-
August 15, 2019 at 8:33 amPython: Pandas Lesson 11 | Ali's Photography Space...
-
August 18, 2019 at 8:40 amPython: Pandas Lesson 12 | Ali's Photography Space...
-
August 25, 2019 at 9:22 amlearning – Temp | Ali's Photography Space...