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
方法遇到内存问题,而myshift
方法仍然有效
test = test.ffill()
test = test.fillna(method='ffill')