Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print Specific Rows and Columns in Pandas

I am having trouble printing certain values within my CSV.

I have a file that has 9 columns

Record_id   Month   Day   Year  Location_id   Animal_id  Sex   Length   Weight

and over 1000 rows.

I want to print Month , Day , and Year columns when the year is equivalent to 2002.

Because I have a lot of data I decided to only work with the first 5 rows where year is equal to 2002.

This is my code:

data.df.iloc[0:5, 1:4]

With this I can print the first 5 rows and the 3 columns I desire. However I can't figure out how to filter the year to be 2002

like image 967
royalblue Avatar asked Apr 29 '18 15:04

royalblue


People also ask

How do I print specific column values in pandas?

By using the to_string() function, we are able to print only the values in the points column without the column header or the row index values.


1 Answers

you can start by getting all the rows where year equal to 2002 with

filtered_data = df[df["Year"]==2002]

then you can apply your code to get only the first five rows and the three selected columns with

filtered_data.iloc[0:5, 1:4]
like image 177
abdelwaheb moalla Avatar answered Sep 23 '22 07:09

abdelwaheb moalla