如何将NaN数组插入numpy 2D数组 [英] How to insert NaN array into a numpy 2D array

查看:72
本文介绍了如何将NaN数组插入numpy 2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在特定位置的2D数组中插入任意数量的NaN值行。我正在从.csv文件中的微控制器记录一些数据,并使用python进行解析。

I'm trying to insert an arbitrary number of rows of NaN values within a 2D array at specific places. I'm logging some data from a microcontroller in a .csv file and parsing with python.

数据存储在这样的3列2D数组中

The data is stored in a 3 column 2D array like this

[(122.0, 1.0, -47.0) (123.0, 1.0, -47.0) (125.0, 1.0, -44.0) ..., 
(39.0, 1.0, -47.0) (40.0, 1.0, -45.0) (41.0, 1.0, -47.0)]

第一列是一个序列计数器。我想做的是遍历序列值,diff当前和先前的序列号,并插入与缺少缺失序列一样多的nan行。

The first column is an sequence counter. What I'm trying to do is iterate through the sequence values, diff current and previous sequence number and insert as many rows with nan as there are missing sequences.

基本上,

[(122.0, 1.0, -47.0) (123.0, 1.0, -47.0) (125.0, 1.0, -44.0)]

将成为

[(122.0, 1.0, -47.0) (123.0, 1.0, -47.0) (nan, nan, nan) (125.0, 1.0, -44.0)]

但是以下 np.insert 的实现会产生错误

However the following implementation of np.insert produces an error

while (i < len(list[1])):
     pid = list[i][0]
     newMissing = (pid - LastGoodId + 255) % 256
     TotalMissing = TotalMissing + newMissing
     np.insert(list,i,np.zeros(newMissing,1) + np.nan)   
     i = i + newMissing
     list[i][0] = TotalMissing
     LastGoodId = pid 




---> 28 np.insert(list,i,np.ze ros(newMissing,1)+ np.nan)
29 i = i + newMissing
30 list [i] [0] = TotalMissing

---> 28 np.insert(list,i,np.zeros(newMissing,1) + np.nan) 29 i = i + newMissing 30 list[i][0] = TotalMissing

TypeError:无法理解数据类型

TypeError: data type not understood

关于如何实现此目标的任何想法?

Any ideas on how I can accomplish this?

推荐答案

来自 np.insert() 的文档:

From the doc of np.insert():

import numpy as np
a = np.arrray([(122.0, 1.0, -47.0), (123.0, 1.0, -47.0), (125.0, 1.0, -44.0)]))
np.insert(a, 2, np.nan, axis=0)
array([[ 122.,    1.,  -47.],
       [ 123.,    1.,  -47.],
       [  nan,   nan,   nan],
       [ 125.,    1.,  -44.]])

这篇关于如何将NaN数组插入numpy 2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆