Home > Enterprise >  Pandas: adjust value of DataFrame that is sliced multiple times
Pandas: adjust value of DataFrame that is sliced multiple times

Time:03-05

Imagine I have the follow Pandas.DataFrame:

df = pd.DataFrame({
    'type': ['A', 'A', 'A', 'B', 'B', 'B'],
    'value': [1, 2, 3, 4, 5, 6]  
})

I want to adjust the first value when type == 'B' to 999, i.e. the fourth row's value should become 999.

Initially I imagined that

df.loc[df['type'] == 'B'].iloc[0, -1] = 999 

or something similar would work. But as far as I can see, slicing the df twice does not point to the original df anymore so the value of the df is not updated.

My other attempt is

df.loc[df.loc[df['type'] == 'B'].index[0], df.columns[-1]] = 999

which works, but is quite ugly.

So I'm wondering -- what would be the best approach in such situation?

CodePudding user response:

Instead of df, you could filter the mask:

msk = df['type'] == 'B'
df.loc[msk[msk].index[0], 'value'] = 999

or even uglier:

df.loc[msk, 'value'] = [999]   df.loc[msk, 'value'].tolist()[1:]

Output:

  type  value
0    A      1
1    A      2
2    A      3
3    B    999
4    B      5
5    B      6

CodePudding user response:

You can use idxmax which returns the index of the first occurrence of a max value. Like this using a boolean series:

df.loc[(df['type'] == 'B').idxmax(), 'value'] = 999

Output:

   type  value
0    A      1
1    A      2
2    A      3
3    B    999
4    B      5
5    B      6
  • Related