相关疑难解决方法(0)

如何找到滞后过零点?

在numpy中,我想检测信号从(先前已经)低于某个阈值的点到高于某个其他阈值.这适用于诸如去抖动或在存在噪声等情况下的准确过零点之类的事情.

像这样:

import numpy

# set up little test problem
N = 1000
values = numpy.sin(numpy.linspace(0, 20, N))
values += 0.4 * numpy.random.random(N) - 0.2
v_high = 0.3
v_low = -0.3

# find transitions from below v_low to above v_high    
transitions = numpy.zeros_like(values, dtype=numpy.bool)

state = "high"

for i in range(N):
    if values[i] > v_high:
        # previous state was low, this is a low-to-high transition
        if state == "low":
            transitions[i] = True
        state = "high"
    if values[i] < v_low:
        state …
Run Code Online (Sandbox Code Playgroud)

python numpy debouncing

11
推荐指数
1
解决办法
4497
查看次数

使用最后的非零值填充1d numpy数组的零值

假设我们有一个填充了一些int值的1d numpy数组.让我们说其中一些是0.

有没有办法,使用numpy数组的功能,0用找到的最后一个非零值填充所有值?

例如:

arr = np.array([1, 0, 0, 2, 0, 4, 6, 8, 0, 0, 0, 0, 2])
fill_zeros_with_last(arr)
print arr

[1 1 1 2 2 4 6 8 8 8 8 8 2]
Run Code Online (Sandbox Code Playgroud)

一种方法是使用此功能:

def fill_zeros_with_last(arr):
    last_val = None # I don't really care about the initial value
    for i in range(arr.size):
        if arr[i]:
            last_val = arr[i]
        elif last_val is not None:
            arr[i] = last_val
Run Code Online (Sandbox Code Playgroud)

但是,这是使用原始python for循环而不是利用numpy和 …

python numpy

11
推荐指数
2
解决办法
3296
查看次数

水平(沿行)向前填充 Pandas 数据框,而不向前填充每行中的最后一个值

我有一个 Pandas 数据框,我想水平转发填充,但我不想转发填充超过每行中的最后一个条目。这是一些已停产的产品的时间序列定价数据,因此我不希望将记录的最后一个值向前填充到当前值。

FWDFILL.apply(lambda series: series.iloc[:,series.last_valid_index()].ffill(axis=1))
Run Code Online (Sandbox Code Playgroud)

^我包含的代码做了我想要的,但它是垂直的。这可能可以帮助人们作为起点。

>>> print(FWDFILL)

1      1     NaN     NaN     2     NaN  
2     NaN     1      NaN     5     NaN  
3     NaN     3       1     NaN    NaN  
4     NaN    NaN     NaN    NaN    NaN  
5     NaN     5      NaN    NaN     1  
Run Code Online (Sandbox Code Playgroud)

期望输出:

1      1      1      1     2     NaN  
2     NaN     1      1     5     NaN  
3     NaN     3      1    NaN    NaN  
4     NaN    NaN    NaN   NaN    NaN  
5     NaN     5      5     5      1
Run Code Online (Sandbox Code Playgroud)

python numpy pandas

5
推荐指数
1
解决办法
1081
查看次数

删除pandas中2个特定列之间的空值

我有以下时间序列数据帧.我想用之前的值填充缺失的值.但是我只想填充first_valid_index和last_valid索引之间的缺失值.所以我想要填充的列对于每一行都是不同的.我怎样才能做到这一点?

所以,给定这个数据帧.

import numpy as np
import pandas as pd
df = pd.DataFrame([[1, 2 ,3,np.nan,5], [1, 3 , np.nan , 4 , np.nan], [4, np.nan , 7 , np.nan,np.nan]], columns=[2007,2008,2009,2010,2011])
Run Code Online (Sandbox Code Playgroud)

输入数据帧:

    2007    2008    2009    2010    2011
     1       2       3      NaN     5
     1       3       NaN    4       NaN
     4       Nan     7      NaN     NaN     
Run Code Online (Sandbox Code Playgroud)

输出数据帧:

2007    2008    2009    2010    2011
 1       2       3        3      5
 1       3       3        4      NaN
 4       4       7        NaN    NaN
Run Code Online (Sandbox Code Playgroud)

我想为first_valid_index和last_valid_index创建新列,然后使用.apply(),但是如何在每行填充不同的列?

def fillMissing(x):
    first_valid = int(x["first_valid"]) …
Run Code Online (Sandbox Code Playgroud)

python pandas

3
推荐指数
1
解决办法
65
查看次数

标签 统计

python ×4

numpy ×3

pandas ×2

debouncing ×1