How to properly remove NaN values from table

落爺英雄遲暮 提交于 2019-12-10 17:45:19

问题


After reading an Excel spreadsheet in Matlab I unfortunately have NaNs included in my resulting table. So for example this Excel table:

would result in this table:

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),:);

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?


回答1:


Either this:

tab = tab(~any(ismissing(tab),2),:);

or:

tab = rmmissing(tab);

if you want to remove rows that contain one or more missing value.

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.

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.



来源:https://stackoverflow.com/questions/47758228/how-to-properly-remove-nan-values-from-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!