如何从表中正确删除NaN值 [英] How to properly remove NaN values from table
问题描述
在Matlab中阅读Excel电子表格后,很遗憾,我的结果表中包含了NaN.举例来说,这个Excel表格:
After reading an Excel spreadsheet in Matlab I unfortunately have NaNs included in my resulting table. So for example this Excel table:
将在此表中显示结果
出现另一列NaN的地方.我尝试使用以下代码段删除NaN:
where an additional column of NaNs occurs. I tried to remove the NaNs with the following code snippet:
measurementCells = readtable('MWE.xlsx','ReadVariableNames',false,'ReadRowNames',true);
measurementCells = measurementCells(any(isstruct(measurementCells('TIME',1)),1),:);
但是,这将导致生成0x6
表,而不再显示任何值.如何在不删除表中任何数据的情况下正确删除NaN?
However this results in a 0x6
table, without any values present anymore. How can I properly remove the NaNs without removing any data from the table?
推荐答案
要么:
tab = tab(~any(ismissing(tab),2),:);
或:
tab = rmmissing(tab);
如果要删除包含一个或多个缺失值的行.
if you want to remove rows that contain one or more missing value.
如果您想用其他值替换缺少的值,请阅读有关fillmissing
( https://mathworks.com/help/matlab/ref/standardizemissing.html )功能正常运行.这些示例是详尽无遗的,可以帮助您找到最适合您需求的解决方案.
If you want instead to replace missing values with other values, read about how fillmissing
(https://mathworks.com/help/matlab/ref/fillmissing.html) and standardizeMissing
(https://mathworks.com/help/matlab/ref/standardizemissing.html) functions work. The examples are exhaustive and should help you to find the solution that best fits your needs.
最后一个解决方案是使用EmptyValue
参数在调用readtable
函数的过程中发现(并以自己喜欢的方式进行操作)NaN
值.但这仅适用于数字数据.
One last solution you have is to spot (and manipulate in the way you prefer) NaN
values within the call to the readtable
function using the EmptyValue
parameter. But this works only against numeric data.
这篇关于如何从表中正确删除NaN值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!