Pandas - Replace values based on index Pandas - Replace values based on index pandas pandas

Pandas - Replace values based on index


Use loc:

df.loc[0:15,'A'] = 16print (df)     A   B0   16  451   16   52   16  973   16  584   16  265   16  876   16  517   16  178   16  399   16  7310  16  9411  16  6912  16  5713  16  2414  16  4315  16  7716  41   017   3  2118   0  9819  45  3920  66  6221   8  5322  69  4723  48  53

Solution with ix is deprecated.


In addition to the other answers, here is what you can do if you have a list of individual indices:

indices = [0,1,3,6,10,15]df.loc[indices,'A'] = 16print(df.head(16))

Output:

     A  B0   16  41   16  42    4  33   16  44    1  15    3  06   16  47    2  18    4  49    3  410  16  011   3  112   4  213   2  214   2  115  16  1


One more solution is

df.at[0:15, 'A']=16print(df.head(20))

OUTPUT:

     A   B0   16  441   16  862   16  973   16  794   16  945   16  246   16  887   16  438   16  649   16  3910  16  8411  16  4212  16   813  16  7214  16  2315  16  2816  18  1117  76  1518  12  3819  91   6