0

I have a dataframe which contains details of certain courses. In that I have a column time_period which is the duration if course and a 'type' column which is the type of course.

I am iterating over that column by -

for year in df_course['time_period']:

Now I have to see if there is row where 'year' == 6 and the type of course is 'Post Graduate' i.e.

for year in df_course['time_period']:
    if year == 6 and df_course['type'] == 'Post Graduate':
        #do something

But I am not able to access the row value of 'type' column inside the 'time_period' loop column. How to proceed?

Prakket
  • 15
  • 6
  • Depending on what you want to do, iteration is most likely not what you need. I encourage you to read how to perform a [conditional creation of a series/dataframe column](https://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column). This might be what you want. – mozway Feb 02 '23 at 08:56
  • That really helped. Just a quick question, If I set the conditions and choices will It apply to all rows (default value) or the rows where only the conditions met? – Prakket Feb 02 '23 at 09:59
  • With `numpy.where`/`numpy.select` all rows are computed, then selected. If you have an expensive check, rather create groups and use `groupby.apply` – mozway Feb 02 '23 at 10:00

0 Answers0