Question: I have a dataframe that contains nan values in particular column. while iterating through the rows, if it come across `nan`(using `isnan()` method) then I need to change it to some other value(since I have some conditions). I tried using `replace()` and `fillna()` with `limit` parameter also but they are modifying whole column when they come across the first `nan` value? Is there any method that I can assign value to specific `nan` rather than changing all the values of a column? 

Example: the dataframe looks like it: 

```
  points  sundar    cate  king  varun  vicky  john  charlie  target_class
1     x2       5   'cat'     4     10      3     2        1           NaN
2     x3       3   'cat'     1      2      3     1        1           NaN
3     x4       6  'lion'     8      4      3     7        1           NaN
4     x5       4  'lion'     1      1      3     1        1           NaN
5     x6       8   'cat'    10     10      9     7        1           0.0

```

an I have a list like 

```
a = [1.0, 0.0]

```

and I expect to be like 

```
  points  sundar    cate  king  varun  vicky  john  charlie  target_class
1     x2       5   'cat'     4     10      3     2        1           1.0
2     x3       3   'cat'     1      2      3     1        1           1.0
3     x4       6  'lion'     8      4      3     7        1           1.0
4     x5       4  'lion'     1      1      3     1        1           0.0
5     x6       8   'cat'    10     10      9     7        1           0.0

```

I wanted to change the target\_class values based on some conditions and assign values of the above list.

Answer: 
I believe need replace `NaN`s values to `1` only for indexes specified in list `idx`:

```
mask = df['target_class'].isnull()
idx = [1,2,3]
df.loc[mask, 'target_class'] = df[mask].index.isin(idx).astype(int)
print (df)
  points  sundar    cate  king  varun  vicky  john  charlie  target_class
1     x2       5   'cat'     4     10      3     2        1           1.0
2     x3       3   'cat'     1      2      3     1        1           1.0
3     x4       6  'lion'     8      4      3     7        1           1.0
4     x5       4  'lion'     1      1      3     1        1           0.0
5     x6       8   'cat'    10     10      9     7        1           0.0

```

Or:

```
idx = [1,2,3]
s = pd.Series(df.index.isin(idx).astype(int), index=df.index)
df['target_class'] = df['target_class'].fillna(s)

```

EDIT:

From comments solution is assign values by index and columns values with [`DataFrame.loc`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html):

```
df2.loc['x2', 'target_class'] = list1[0]

```
I suppose your conditions for imputing the `nan` values does not depend on the number of them in a column. In the code below I stored all the imputation rules in one function that receives as parameters the entire row (containing the `nan`) and the column you are investigating for. If you also need all the dataframe for the imputation rules, just pass it through the `replace_nan` function. In the example I imputate the `col` element with the mean values of the other columns.

```
import pandas as pd
import numpy as np

def replace_nan(row, col):
    row[col] = row.drop(col).mean()
    return row

df = pd.DataFrame(np.random.rand(5,3), columns = ['col1', 'col2', 'col3'])

col_to_impute = 'col1'
df.loc[[1, 3], col_to_impute] = np.nan

df = df.apply(lambda x: replace_nan(x, col_to_impute) if np.isnan(x[col_to_impute]) else x, axis=1)

```