Skip to main content
 首页 » 编程设计

python之R 到 Python : Using Shift with ifelse

2024年10月01日8youxin

示例数据:

test = structure(list(A = 1:16, B = c(".", NA, NA, NA, ".", NA, NA,  
NA, ".", NA, NA, NA, ".", NA, NA, NA), C = c(6L, NA, NA, NA,  
6L, NA, NA, NA, 6L, NA, NA, NA, 6L, NA, NA, NA), D = c(58, 59,  
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), E = c(0.945252,  
0.949158, 0.945252, 0.945252, 0.945252, 0.945252, 0.945252, 0.949158,  
0.949158, 0.949158, 0.949158, 0.945252, 0.945252, 0.945252, 0.945252,  
0.945252), F = c(62.4375, NA, 62.34375, NA, 62.28125, NA, 62.28125,  
NA, 62.25, NA, 62.21875, NA, 62.25, NA, 62.28125, NA)), .Names = c("A",  
"B", "C", "D", "E", "F"), row.names = c(NA, 16L), class = "data.frame") 

我在 R 中有上述数据框。我想使用一些逻辑替换 NA 值。

在 R 我写道:

test2 = 测试

库(data.table)

for(m in 1:length(test2)){test2[,m] = ifelse(is.na(test2[,m]),  
                                             ifelse(is.na(shift(test2[,m]))==F, shift(test2[,m]), 
                                                    ifelse(is.na(shift(test2[,m], n=2))==F, shift(test2[,m], n=2), 
                                                           ifelse(is.na(shift(test2[,m], n=3))==F, shift(test2[,m], n=3), 
                                                                  ifelse(is.na(shift(test2[,m], n=4))==F, shift(test2[,m], n=4), 
                                                                         shift(test2[,m], n=5))))) 
                                             , test2[,m])} 

这实现了我想要的。但是现在,我所做的一切都从 R 转换为 Python。除了这个问题,我已经设法翻译了我所有的作品。

我写了等价物:

import numpy as np 
import pandas as pd 
 
for m in range(0, len(test2.columns)): 
    if test2.iloc[:,m].isnull(): 
        if  test2.iloc[:,m].shift(periods=1).notnull(): 
            test2.iloc[:,m] = test2.iloc[:,m].shift(periods=1) 
        else: 
            if test2.iloc[:,m].shift(periods=2).notnull(): 
                 test2.iloc[:,m] = test2.iloc[:,m].shift(periods=2) 
            else: 
                if test2.iloc[:,m].shift(periods=3).notnull(): 
                     test2.iloc[:,m] = test2.iloc[:,m].shift(periods=3) 
                else: 
                    if test2.iloc[:,m].shift(periods=4).notnull(): 
                         test2.iloc[:,m] = test2.iloc[:,m].shift(periods=4) 
                    else: 
                         test2.iloc[:,m] = test2.iloc[:,m].shift(periods=5) 

我意识到这在 Python 中不起作用,甚至没有意义,因为在 R 中,当您将 ifelse 与 shift< 一起使用时 它将逻辑应用到每一行,而在 Python 中,我相信我问的是该系列是 True 还是 False。为了速度,我真的不想遍历整个数据帧,这就是 shift 在 R 中如此出色的原因。我猜可能有一种简单的方法可以在 Python 中使用 If 和 iloc 但我是 Python 菜鸟。

CSV:

 A  B   C   D   E   F 
1   .   6   58  0.945252    62.4375 
2           59  0.949158     
3           0   0.945252    62.34375 
4           1   0.945252     
5   .   6   2   0.945252    62.28125 
6           3   0.945252     
7           4   0.945252    62.28125 
8           5   0.949158     
9   .   6   6   0.949158    62.25 
10          7   0.949158     
11          8   0.949158    62.21875 
12          9   0.945252     
13  .   6   10  0.945252    62.25 
14          11  0.945252     
15          12  0.945252    62.28125 
16          13  0.945252     

请您参考如下方法:

你可以使用 DataFrame.ffill , 它比调用 fillna 要短一些。

test = test.ffill() 

它所做的是用每行中第一个连续的非空值向前填充所有 NaN。或者,您可以使用 method='ffill' 调用 fillna 来做同样的事情:

test = test.fillna(method='ffill') 

这与 method='pad' 的作用相同。