Home > front end >  Replace part of df column with values defined in Series/dictionary
Replace part of df column with values defined in Series/dictionary

Time:11-03

I have a column in a DataFrame that often has repeat indexes. Some indexes have exceptions and need to be changed based on another Series I've made, while the rest of the indices are fine as is. The Series indices are unique.

Here's a couple variables to illustrate

df = pd.DataFrame(data={'hi':[1, 2, 3, 4, 5, 6, 7]}, index=[1, 1, 1, 2, 2, 3, 4])
Out[52]: 
   hi
1   1
1   2
1   3
2   4
2   5
3   6
4   7

exceptions = pd.Series(data=[90, 95], index=[2, 4])
Out[36]: 
2    90
4    95

I would like to do set the df to ...

   hi
1   1
1   2
1   3
2  90
2  90
3   6
4  95

What's a clean way to do this? I'm a bit new to Pandas, my thoughts are just to loop but I don't think that's the proper way to solve this

CodePudding user response:

Assuming that the index in exceptions is guaranteed to be a subset of df indexes we can use loc and the Series.index to assign the values:

df.loc[exceptions.index, 'hi'] = exceptions

We can use index.intersection if we have extra values in exceptions that does not or should not align in df:

exceptions = pd.Series(data=[90, 95, 100], index=[2, 4, 5])
df.loc[exceptions.index.intersection(df.index, sort=False), 'hi'] = exceptions

df:

   hi
1   1
1   2
1   3
2  90
2  90
3   6
4  95
  • Related