繁体   English   中英

根据熊猫的状况替换某些值

[英]Replace certain values depending on condition in pandas

我正在尝试将以下R代码转换为Python并由于行索引而卡住...

df$col3[index+1] <− df$col2[index] # what he want :col2 in certain index  assign its value to col3 by index increment 1.

虚构的例子

df = pd.DataFrame({'id' : [1, 1, 1, 2, 2, 3, 4, 4, 4, 4, 5, 5], 
'id_old' : [1, 1, 2, 2, 3, 4, 4, 4, 4, 5, 5, 5], 
'col1' : np.random.normal(size = 12), 
'col2' : np.random.randint(low = 20, high = 50, size = 12), 
'col3' : np.repeat(20, 12)})
print(df)

myindex = np.where(df.id != df.id_old) # tuple
print(myindex)
print(np.add(myindex, 1))
replacement_values = df.iloc[myindex][['col2']]

产量

    id  id_old      col1  col2  col3
0    1       1  0.308380    23    20
1    1       1  1.185646    35    20
2    1       2 -0.432066    27    20
3    2       2  0.115055    32    20
4    2       3  0.683291    34    20
5    3       4 -1.916321    42    20
6    4       4  0.888327    34    20
7    4       4  1.312879    29    20
8    4       4  1.260612    27    20
9    4       5  0.062635    22    20
10   5       5  0.081149    23    20
11   5       5 -1.872873    32    20

(array([2, 4, 5, 9]),)
[[ 3  5  6 10]]

这是我尝试的:

df.loc[np.add(myindex, 1), 'col3'] = replacement_values
df.loc[df.index.isin(np.add(myindex + 1)), 'col3'] = replacement_values

所需结果:

    id  id_old      col1  col2  col3
0    1       1  0.308380    23    20
1    1       1  1.185646    35    20
2    1       2 -0.432066    27    20
3    2       2  0.115055    32    27
4    2       3  0.683291    34    20
5    3       4 -1.916321    42    34
6    4       4  0.888327    34    42
7    4       4  1.312879    29    20
8    4       4  1.260612    27    20
9    4       5  0.062635    22    20
10   5       5  0.081149    23    22
11   5       5 -1.872873    32    20

我想我正在忽略一些基本知识,还是我完全走错了道路?

非常感谢你的帮助!

修正你的代码中,添加values data.frameR是没有索引敏感,但在pandas ,无论指数做

df=pd.read_clipboard()
df.loc[np.add(myindex, 1)[0],'col3']=df.iloc[myindex]['col2'].values
df
Out[399]: 
    id  id_old      col1  col2  col3
0    1       1  0.308380    23    20
1    1       1  1.185646    35    20
2    1       2 -0.432066    27    20
3    2       2  0.115055    32    27
4    2       3  0.683291    34    20
5    3       4 -1.916321    42    34
6    4       4  0.888327    34    42
7    4       4  1.312879    29    20
8    4       4  1.260612    27    20
9    4       5  0.062635    22    20
10   5       5  0.081149    23    22
11   5       5 -1.872873    32    20

不知道为什么熊猫需要用R进行如此简单的操作,但是用mask / where + shift + fillna

df['col3'] = (
    df.col2.where(df.id != df.id_old).shift().fillna(df.col3).astype(int)
)

df
    id  id_old      col1  col2  col3
0    1       1  0.308380    23    20
1    1       1  1.185646    35    20
2    1       2 -0.432066    27    20
3    2       2  0.115055    32    27
4    2       3  0.683291    34    20
5    3       4 -1.916321    42    34
6    4       4  0.888327    34    42
7    4       4  1.312879    29    20
8    4       4  1.260612    27    20
9    4       5  0.062635    22    20
10   5       5  0.081149    23    22
11   5       5 -1.872873    32    20

IIUC

mask = (df.id_old - df.id).shift().fillna(0).astype(bool)
df.loc[mask, "col3"] = df.loc[mask, "col2"]

暂无
暂无

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

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