0

I've figured out how to iterate through a specific column within a csv file with n columns. But now I want to create a conditional and I'm not sure how.

The column in question is a column of days of the week (Monday, Tuesday, etc.). I want to write code so that my iteration counts all the Mondays, all the Tuesdays, so and so forth. This is what I wrote up and I get a syntax error.

mon = 0
# print (df)

days_week = df.iloc[4:,2]
# print(days_week)
for i in days_week:
    if i == "MONDAY"
    mon+=1

The data type for this particular column right now 'object'. So I figure I need to change it to strings?

1
  • 1
    The syntax for if is very like that of for loops -- you'll need a colon : at the end of the if statement, and to indent the next line. But JALO's answer solves everything nicely. Commented Jun 28, 2020 at 18:00

1 Answer 1

1

Check pandas value_counts function for series data:

value_counts

e.g. say df['days'] contains your data, then:

df['days'].value_counts() will give you counts for each of the days.

Sign up to request clarification or add additional context in comments.

2 Comments

Unbelievable, thank you so much! This is so much more efficient than what I was thinking of doing :)
You are most welcome! There are many features in pandas which you will be pleasantly surprised to experience as you go forward. If you need any help, the community is anyways there :)

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.