Change Value in pandas dataframe with conditions and index

70 views Asked by At

I do not understand why the following code does not work :

df_sensor.loc[(df_sensor.user_id == labels_user_id) & (df_sensor.exp_id == labels_exp_id),'activity_id'].iloc[start:end] = labels['activity_id'][I]

This line

df_sensor.loc[(df_sensor.user_id == labels_user_id) & (df_sensor.exp_id == labels_exp_id),'activity_id'].iloc[start:end]

Return this data frame

Dataframe

I want to change the value where user_id and exp_id from specific index (start and end)

EDIT

I have 2 dataframe

1: Dataframe 1

2: Dataframe 2

I want to change the activity_id of the DF1 from the DF2.activity_id with start and end as an index

1

There are 1 answers

0
ALollz On

Your issue is chained assignment: df.loc[].iloc[] = which will not change the DataFrame. However your selection is complicated, you want to change a range of values that are determined only after the initial slice.

We can define your initial mask and use some math with cumsum to allow for the same selection with a single .loc call.


Sample Data

  name  year  value
0    A  2010      1
1    A  2011      2
2    B  2014      5
3    A  2012      3
4    A  2013      4

### Illustrate the problem
start = 1
end = 3
df.loc[df.name.eq('A'), 'name'].iloc[start:end] = 'foo'

# Nothing changes...
print(df)
#  name  year  value
#0    A  2010      1
#1    A  2011      2
#2    B  2014      5
#3    A  2012      3
#4    A  2013      4

Code

# Define your initial `.loc` condition
m = df.name.eq('A')

# Keep only True within the selection range. `where` logic allow for gaps.
m = (m.cumsum().gt(start) & m.cumsum().le(end)).where(m).fillna(False).astype(bool)

# Original df.loc[].iloc[] is a single selection:
df.loc[m, 'name'] = 'foo'

print(df)
#  name  year  value
#0    A  2010      1
#1  foo  2011      2
#2    B  2014      5
#3  foo  2012      3
#4    A  2013      4