简体   繁体   English

在 Pandas 中同时使用 loc 和 iloc

[英]Use loc and iloc together in pandas

Say I have the following dataframe, and I want to change the two elements in column c that correspond to the first two elements in column a that are equal to 1 to equal 2 .说我有以下数据帧,我想在列中的两个元件改变c对应于在列中的前两个元素a是等于1为等于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.第二行中的代码不起作用,因为 iloc 设置了副本,因此不会修改原始数据帧。 How would I do this?我该怎么做?

一种肮脏的方式是:

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

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 0 without 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

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

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