Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I have a pandas data frame that looks like this (its a pretty big one)

           date      exer exp     ifor         mat  

1092  2014-03-17  American   M  528.205  2014-04-19 

1093  2014-03-17  American   M  528.205  2014-04-19 

1094  2014-03-17  American   M  528.205  2014-04-19 

1095  2014-03-17  American   M  528.205  2014-04-19    

1096  2014-03-17  American   M  528.205  2014-05-17 

now I would like to iterate row by row and as I go through each row, the value of ifor in each row can change depending on some conditions and I need to lookup another dataframe.

Now, how do I update this as I iterate. Tried a few things none of them worked.

for i, row in df.iterrows():

    if <something>:

        row['ifor'] = x

    else:

        row['ifor'] = y

    df.ix[i]['ifor'] = x

None of these approaches seem to work. I don't see the values updated in the dataframe.

1 Answer

0 votes
by (41.4k points)

Using df.at() that access a single value for a row/column label pair:

  for i, row in df.iterrows():

      ifor_val = something

      if <condition>:

        ifor_val = something_else

      df.at[i,'ifor'] = ifor_val

To Learn what is data science and how to be a data scientist visit the data scientist course by Intellipaat.

Browse Categories

...