我不明白为什么以下代码不起作用:
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]
此行
df_sensor.loc[(df_sensor.user_id == labels_user_id) & (df_sensor.exp_id == labels_exp_id),'activity_id'].iloc[start:end]
返回此数据框
我想更改特定索引(开始和结束)中user_id和exp_id的值
编辑
我有2个数据框
1: Dataframe 1
2: Dataframe 2
我想用开始和结束作为索引从DF2.activity_id更改DF1的activity_id
答案 0 :(得分:0)
您的问题是链接分配:df.loc[].iloc[] =
,这不会更改DataFrame。但是,您的选择很复杂,您希望更改仅在初始切片之后确定的值范围。
我们可以定义您的初始掩码,并使用cumsum
进行一些数学运算,以通过一次.loc
调用进行相同的选择。
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
# 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