根据条件和索引更改熊猫数据框中的值

问题描述 投票:0回答:1

我不明白为什么以下代码不起作用:

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]

返回此数据框

Dataframe

我想从特定索引(开始和结束)更改user_id和exp_id的值

编辑

我有2个数据框

1:Dataframe 1

2:Dataframe 2

我想用开始和结束作为索引从DF2.activity_id更改DF1的activity_id

python pandas dataframe anaconda
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.