熊猫系列-跌落第一价值的问题

时间:2020-04-26 18:19:01

标签: python-3.x pandas

我无法删除我的系列中的第一个NaN值。

我将正常系列的差值与1个周期的移位系列进行了比较。这是我的计算方式:

x[c] = x[c] - x[c].shift(periods=1)

当我尝试使用以下方法删除第一个值时:

  • x [c] .drop(labels = [0])
  • x [c] .dropna()
  • x [c] .iloc [1:]

在我的调任中不起作用

# these are not used all together, but separately
x[c] = x[c].dropna()
x[c] = x[c].drop(labels=['1981-09-29'])
x[c] = x[c][1:]
print(x[c])
Date
1981-09-29         NaN
1981-09-30   -0.006682
1981-10-01   -0.014575
1981-10-02   -0.004963
1981-10-05   -0.004963

但是,当我在打印语句中调用drop或dropna函数时,它会起作用!

print(x[c].dropna())
Date
1981-09-30   -0.006682
1981-10-01   -0.014575
1981-10-02   -0.004963
1981-10-05   -0.004963
1981-10-06   -0.005514

什么方法都没有关系,我只想摆脱系列中的第一个元素。 请帮助。

3 个答案:

答案 0 :(得分:0)

尝试一下?


x[c] = x[c].dropna(inplace=True)
print(x[c])

答案 1 :(得分:0)

如果您知道它是不需要的第一个元素,则最简单的方法是使用series.iloc[1:, :]dataframe.iloc[1:, :]

答案 2 :(得分:0)

数据框有多个序列,如果我仅尝试重新分配一个序列,它仍然会给我NaN值。这是因为Series在数据帧中的长度必须相同。因此,在执行计算之后,我需要在数据帧上调用dropna