简体   繁体   English

根据数据帧切片的条件更改值

[英]Change value based on condition on slice of dataframe

I have a dataframe like this:我有一个这样的数据框:

df = pd.DataFrame(columns=['Dog', 'Small', 'Adult'])
df.Dog = ['Poodle', 'Shepard', 'Bird dog','St.Bernard']
df.Small = [1,1,0,0]
df.Adult = 0

That will look like this:看起来像这样:

       Dog         Small   Adult
0      Poodle          1   0
1      Shepard         1   0
2      Bird dog        0   0
3      St.Bernard      0   0

Then I would like to change one column based on another.然后我想根据另一列更改一列。 I can do that:我可以这样做:

df.loc[df.Small == 0, 'Adult'] = 1

However, I just want to do so for the 3 first rows.但是,我只想对前 3 行这样做。

I can select the first three rows:我可以选择前三行:

df.iloc[0:2]

But if I try to change values on the first three rows:但是,如果我尝试更改前三行的值:

df.iloc[0:2, df.Small == 0, 'Adult'] = 1

I get an error.我得到一个错误。

I also get an error if I merge the two:如果将两者合并,我也会收到错误消息:

df.iloc[0:2].loc[df.Small == 0, 'Adult'] = 1

It tells me that I am trying to set a value on a copy of a slice.它告诉我我正在尝试在切片的副本上设置一个值。

How should I do this correctly?我应该如何正确执行此操作?

You could include the range as another condition in your .loc selection (for the general case, I'll explicitly include the 0):您可以在.loc选择中包括该范围作为另一个条件(对于一般情况,我将明确包括 0):

df.loc[(df.Small == 0) & (0 <= df.index) & (df.index <= 2), 'Adult'] = 1

Another option is to transform the index into a series to use pd.Series.between :另一种选择是将索引转换为系列以使用pd.Series.between

df.loc[(df.Small == 0) & (df.index.to_series().between(0, 2)), 'Adult'] = 1

adding conditionals based on index works only if the index is already sorted.只有当索引已经排序时,基于索引添加条件才有效。 Alternatively, you can do the following:或者,您可以执行以下操作:

ind = df[df.Small == 0].index[:2]
df.loc[ind, 'Adult'] = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM