简体   繁体   中英

How to extract non nan values from a dataframe

I want to extract n consecutive cells (without taking into account the non values) from a dataframe like the following:

在此处输入图像描述

We can have the following dataframes for n=2:

在此处输入图像描述

or

在此处输入图像描述

Consider

>>> df 
     0    1     2   3
0  0.0  1.0   NaN  23
1  NaN  1.0  23.0   4
2  NaN  NaN   1.0   0
3  0.0  NaN   NaN   1
>>> df.apply(lambda s: s.dropna().iloc[:2].reset_index(drop=True), axis=1) 
     0     1
0  0.0   1.0
1  1.0  23.0
2  1.0   0.0
3  0.0   1.0

edit: this will give you the first two (or n ) non missing values from each row. Your question is unclear on whether this is sufficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM