在 Pandas 中同时使用 loc 和 iloc

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37911147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:25:06  来源:igfitidea点击:

Use loc and iloc together in pandas

pythonpandasindexingdataframe

提问by Alex

Say I have the following dataframe, and I want to change the two elements in column cthat correspond to the first two elements in column athat are equal to 1to equal 2.

假设我有以下数据框,我想更改 columnc中对应于 column中前两个元素的两个元素a等于1equal 2

>>> df = pd.DataFrame({"a" : [1,1,1,1,2,2,2,2], "b" : [2,3,1,4,5,6,7,2], "c" : [1,2,3,4,5,6,7,8]})
>>> df.loc[df["a"] == 1, "c"].iloc[0:2] = 2
>>> df
   a  b  c
0  1  2  1
1  1  3  2
2  1  1  3
3  1  4  4
4  2  5  5
5  2  6  6
6  2  7  7
7  2  2  8

The code in the second line doesn't work because iloc sets a copy, so the original dataframe is not modified. How would I do this?

第二行中的代码不起作用,因为 iloc 设置了副本,因此不会修改原始数据帧。我该怎么做?

采纳答案by jezrael

You can use Index.isin:

您可以使用Index.isin

import pandas as pd

df = pd.DataFrame({"a" : [1,1,1,1,2,2,2,2], 
                   "b" : [2,3,1,4,5,6,7,2],
                   "c" : [1,2,3,4,5,6,7,8]})

#more general index                       
df.index = df.index + 10
print (df)
    a  b  c
10  1  2  1
11  1  3  2
12  1  1  3
13  1  4  4
14  2  5  5
15  2  6  6
16  2  7  7
17  2  2  8

print (df.index.isin(df.index[:2]))
[ True  True False False False False False False]

df.loc[(df["a"] == 1) & (df.index.isin(df.index[:2])), "c"] = 2
print (df)
    a  b  c
10  1  2  2
11  1  3  2
12  1  1  3
13  1  4  4
14  2  5  5
15  2  6  6
16  2  7  7
17  2  2  8

If index is nice(starts from 0without duplicates):

如果索引是nice(从0没有重复的开始):

df.loc[(df["a"] == 1) & (df.index < 2), "c"] = 2
print (df)
   a  b  c
0  1  2  2
1  1  3  2
2  1  1  3
3  1  4  4
4  2  5  5
5  2  6  6
6  2  7  7
7  2  2  8

Another solution:

另一种解决方案:

mask = df["a"] == 1
mask = mask & (mask.cumsum() < 3)

df.loc[mask.index[:2], "c"] = 2
print (df)
   a  b  c
0  1  2  2
1  1  3  2
2  1  1  3
3  1  4  4
4  2  5  5
5  2  6  6
6  2  7  7
7  2  2  8

回答by ayhan

A dirty way would be:

一种肮脏的方式是:

df.loc[df[df['a']==1][:2].index, 'c'] = 2