Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R到Python:与ifelse一起使用Shift_Python_R_Dataframe - Fatal编程技术网

R到Python:与ifelse一起使用Shift

R到Python:与ifelse一起使用Shift,python,r,dataframe,Python,R,Dataframe,样本数据: 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

样本数据:

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=test

库(data.table)

这达到了我想要的。然而现在,我所做的一切都从
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
noob

CSV:


事实证明,在python中,这相当容易。一旦您认识到我的问题本质上是一个插补问题,我想使用上一个观察结果,我相信它会变得非常简单:

test2 = test.fillna(method = 'pad')
R
中也找到了一种更简单的方法,以备将来使用

library(zoo)
test2 = na.locf(test)
您可以使用,它比调用
fillna
要短一些

test = test.ffill()
它所做的是向前填充所有
NaN
s,每个行中的第一个后续非空值。或者,您可以使用
method='ffill'
调用
fillna
,其作用相同:

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

这与
method='pad'

的作用相同。你的数据集只是触发了我头脑中的蝙蝠侠主题。奥利维亚,我在别处见过的一种跨语言方法是包含CSV文本,以及读取该文本的R和python代码。添加了图片,CSV复制了是的,这是一种方法(但不是最短的方法;-))。应注意,在16 mil行数据帧上,
na.locf
方法遇到内存问题,而my
shift
方法仍然有效
test = test.ffill()
test = test.fillna(method='ffill')