9

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

2
  • Please revise your question to include a Minimal, Complete, and Verifiable example. Commented Apr 29, 2018 at 15:09
  • 1
    df.loc[df.Year==2002,:].iloc[0:5, 1:4] Commented Apr 29, 2018 at 15:10

1 Answer 1

11

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.