Programing

Pandas 데이터 프레임에서 NaN으로 행의 정수 인덱스 찾기

crosscheck 2020. 10. 16. 07:10
반응형

Pandas 데이터 프레임에서 NaN으로 행의 정수 인덱스 찾기


다음과 같은 pandas DataFrame이 있습니다.

                    a         b
2011-01-01 00:00:00 1.883381  -0.416629
2011-01-01 01:00:00 0.149948  -1.782170
2011-01-01 02:00:00 -0.407604 0.314168
2011-01-01 03:00:00 1.452354  NaN
2011-01-01 04:00:00 -1.224869 -0.947457
2011-01-01 05:00:00 0.498326  0.070416
2011-01-01 06:00:00 0.401665  NaN
2011-01-01 07:00:00 -0.019766 0.533641
2011-01-01 08:00:00 -1.101303 -1.408561
2011-01-01 09:00:00 1.671795  -0.764629

NaN이있는 행의 "정수"인덱스를 찾는 효율적인 방법이 있습니까? 이 경우 원하는 출력은 [3, 6].


DataFrame의 경우 df:

import numpy as np
index = df['b'].index[df['b'].apply(np.isnan)]

으로 다시 MultiIndex인덱싱하는 데 사용할 수 있는 다시 제공합니다 df. 예 :

df['a'].ix[index[0]]
>>> 1.452354

정수 인덱스의 경우 :

df_index = df.index.values.tolist()
[df_index.index(i) for i in index]
>>> [3, 6]

다음은 더 간단한 해결책입니다.

inds = pd.isnull(df).any(1).nonzero()[0]

In [9]: df
Out[9]: 
          0         1
0  0.450319  0.062595
1 -0.673058  0.156073
2 -0.871179 -0.118575
3  0.594188       NaN
4 -1.017903 -0.484744
5  0.860375  0.239265
6 -0.640070       NaN
7 -0.535802  1.632932
8  0.876523 -0.153634
9 -0.686914  0.131185

In [10]: pd.isnull(df).any(1).nonzero()[0]
Out[10]: array([3, 6])

그리고 만일을 대비하여 모든 열에 대한 'nan'좌표를 대신 찾으려면 (모두 숫자라고 가정) 여기에 있습니다.

df = pd.DataFrame([[0,1,3,4,np.nan,2],[3,5,6,np.nan,3,3]])

df
   0  1  2    3    4  5
0  0  1  3  4.0  NaN  2
1  3  5  6  NaN  3.0  3

np.where(np.asanyarray(np.isnan(df)))
(array([0, 1]), array([4, 3]))

이것이 너무 늦었는지 모르지만 np.where를 사용하여 다음과 같이 비 값의 인덱스를 찾을 수 있습니다.

indices = list(np.where(df['b'].isna()[0]))

한 줄 솔루션. 그러나 하나의 열에서만 작동합니다.

df.loc[pandas.isna(df["b"]), :].index

다음은 또 다른 간단한 방법입니다.

df = pd.DataFrame([[0,1,3,4,np.nan,2],[3,5,6,np.nan,3,3]])

inds = np.asarray(df.isnull()).nonzero()

(array([0, 1], dtype=int64), array([4, 3], dtype=int64))

NaN 값이있는 행의 모든 ​​인덱스를 찾고있었습니다.
내 작업 솔루션 :

def get_nan_indexes(data_frame):
    indexes = []
    print(data_frame)
    for column in data_frame:
        index = data_frame[column].index[data_frame[column].apply(np.isnan)]
        if len(index):
            indexes.append(index[0])
    df_index = data_frame.index.values.tolist()
    return [df_index.index(i) for i in set(indexes)]

in the case you have datetime index and you want to have the values:

df.loc[pd.isnull(df).any(1), :].index.values

Let the dataframe be named df and the column of interest(i.e. the column in which we are trying to find nulls) is 'b'. Then the following snippet gives the desired index of null in the dataframe:

   for i in range(df.shape[0]):
       if df['b'].isnull().iloc[i]:
           print(i)

Here are tests for a few methods:

%timeit np.where(np.isnan(df['b']))[0]
%timeit pd.isnull(df['b']).nonzero()[0]
%timeit np.where(df['b'].isna())[0]
%timeit df.loc[pd.isna(df['b']), :].index

And their corresponding timings:

333 µs ± 9.95 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
280 µs ± 220 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
313 µs ± 128 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
6.84 ms ± 1.59 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

It would appear that pd.isnull(df['DRGWeight']).nonzero()[0] wins the day in terms of timing, but that any of the top three methods have comparable performance.

참고URL : https://stackoverflow.com/questions/14016247/find-integer-index-of-rows-with-nan-in-pandas-dataframe

반응형