簡體   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