To extract non-nan values ​​from multiple lines in a pandas frame

I am working on several datasets for taxis. I used pandas to combine the entire data set into a single data frame.

My dataframe looks something like this.

                     675                       1039                #and rest 125 taxis
                     longitude     latitude    longitude    latitude
date
2008-02-02 13:31:21  116.56359  40.06489       Nan          Nan
2008-02-02 13:31:51  116.56486  40.06415       Nan          Nan
2008-02-02 13:32:21  116.56855  40.06352       116.58243    39.6313
2008-02-02 13:32:51  116.57127  40.06324       Nan          Nan
2008-02-02 13:33:21  116.57120  40.06328       116.55134    39.6313
2008-02-02 13:33:51  116.57121  40.06329       116.55126    39.6123
2008-02-02 13:34:21  Nan        Nan            116.55134    39.5123

where 675,1039 are taxi identifiers. Basically, a total of 127 taxis with corresponding latitudes and longitudes are broken up in columns.

I have several ways to extract non-zero values ​​for a string.

df.ix[k,df.columns[np.isnan(df.irow(0))!=1]]
              (or)
df.irow(0)[np.isnan(df.irow(0))!=1]
              (or)
df.irow(0)[np.where(df.irow(0)[df.columns].notnull())[0]]

any of the above commands will return,

675   longitude    116.56359
      latitude     40.064890 
4549  longitude    116.34642
      latitude      39.96662
Name: 2008-02-02 13:31:21

now I want to extract all the notnull values ​​from the first few lines (for example, from line 1 to line 6).

how to do it?

I might loop it. But I want him not to go in cycles.

Any help, suggestions are welcome. Thanks in adv! :)

+5
source
3
+4

0.11 (0.11rc1 ), , .iloc, 6 , dropna nan ( dropna , )

, 1: 6, 0: 6 ....

In [8]: df = DataFrame(randn(10,3),columns=list('ABC'),index=date_range('20130101',periods=10))

In [9]: df.ix[6,'A'] = np.nan

In [10]: df.ix[6,'B'] = np.nan

In [11]: df.ix[2,'A'] = np.nan

In [12]: df.ix[4,'B'] = np.nan

In [13]: df.iloc[0:6]
Out[13]: 
                   A         B         C
2013-01-01  0.442692 -0.109415 -0.038182
2013-01-02  1.217950  0.006681 -0.067752
2013-01-03       NaN -0.336814 -1.771431
2013-01-04 -0.655948  0.484234  1.313306
2013-01-05  0.096433       NaN  1.658917
2013-01-06  1.274731  1.909123 -0.289111

In [14]: df.iloc[0:6].dropna()
Out[14]: 
                   A         B         C
2013-01-01  0.442692 -0.109415 -0.038182
2013-01-02  1.217950  0.006681 -0.067752
2013-01-04 -0.655948  0.484234  1.313306
2013-01-06  1.274731  1.909123 -0.289111
+2

Jeff:

import pandas as pd
from numpy.random import randn

df = pd.DataFrame(randn(10,3),columns=list('ABC'),index=pd.date_range('20130101',periods=10))
df.ix[6,'A'] = np.nan
df.ix[6,'B'] = np.nan
df.ix[2,'A'] = np.nan
df.ix[4,'B'] = np.nan

nans , , , :

df = df.fillna(999)

, :

df_nona = df.apply(lambda x: list(filter(lambda y: y != 999, x)))
df_na = df.apply(lambda x: list(filter(lambda y: y == 999, x)))

, , .

df_nona
A    [-1.9804955861, 0.146116306853, 0.359075672435...
B    [-1.01963803293, -0.829747654648, 0.6950551455...
C    [2.40122968044, 0.79395493777, 0.484201174184,...
dtype: object

:

df1 = df.dropna()
index_na  = df.index ^ df1.index
df_na = df[index_na]

In this case, you do not lose information about the index, although it really looks like the previous answers.

Hope this helps!

0
source

All Articles