How to insert values of a new column for some specific rows in Dataframe?

Viewed 826

I have a dataframe and want to insert a new column for it. However, I have to insert this column multiple times, each time for some rows. E.g.

    source   feature 1  feature 2
0     a        xxx         xxx
1     b        xxx         xxx
2     c        xxx         xxx
3     a        xxx         xxx

I want to insert a feature 3. However, for different sources, I have to do it separately.

First, I got the index of df['source'] ==a , and a list of their values E.g.

   index_for_a = [0,3]
   values_for_a = [2, 4]
     source   feature 1  feature 2   feature_3
    0     a        xxx         xxx      2
    1     b        xxx         xxx
    2     c        xxx         xxx
    3     a        xxx         xxx      4

Is there any API I can use to insert the value for multiple specified rows? I found one dataframe.insert(). However, it only works for one specific location so I have to iterate on the rows. Is there any more efficient way to do so?

1 Answers

Use DataFrame.loc for set new values, if no match get missing values:

index_for_a = [0,3]
values_for_a = [2, 4]

df.loc[index_for_a, 'feature_3'] = values_for_a

One possible idea for multiple values:

index_for_a = [0,3]
values_for_a = [2, 4]

index_for_b = [1,2]
values_for_b = [5, 6]

d = dict(zip(index_for_a + index_for_b, values_for_a +values_for_b))
print (d)
{0: 2, 3: 4, 1: 5, 2: 6}

df['new'] = df.index.map(d)
print (df)
  source feature 1 feature 2  feature_3  new
0      a       xxx       xxx        2.0    2
1      b       xxx       xxx        NaN    5
2      c       xxx       xxx        NaN    6
3      a       xxx       xxx        4.0    4
Related