How to check if there are pandas Series?

How to check if there are pandas Series?

I tried this:

How to check if pandas DataFrame is empty?

but it seems that the Series does not have the `isempty 'property.

+6
source share
5 answers

I am using the len function. This is much faster than empty (). len (df.index) is even faster.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10000, 4), columns=list('ABCD'))

def empty(df):
    return df.empty

def lenz(df):
    return len(df) == 0

def lenzi(df):
    return len(df.index) == 0

'''
%timeit empty(df)
%timeit lenz(df)
%timeit lenzi(df)

10000 loops, best of 3: 13.9 µs per loop
100000 loops, best of 3: 2.34 µs per loop
1000000 loops, best of 3: 695 ns per loop

len on index seems to be faster
'''
+14
source

According to the Pandas documentation you need to use a emptyproperty, notisempty

for instance

In [12]: df.empty
Out[13]: False
+6
source

, , - dataFrame :

len(df.col_name.value_counts()) > 0
+5

NDFrame NaNs, - . . .

< >

DataFrame. , :

>>> df_empty = pd.DataFrame({'A' : []})
>>> df_empty
Empty DataFrame
Columns: [A]
Index: []
>>> df_empty.empty
True

If there are only NaNs in our DataFrame , it is not considered empty! We will need to reset the NaNs to make the DataFrame empty:

>>> df = pd.DataFrame({'A' : [np.nan]})
>>> df
    A
0 NaN
>>> df.empty
False
>>> df.dropna().empty
True

Source

+4
source

Thanks @sparrow I used this to check datetime columns:

    if len(df.select_dtypes(include='datetime').iloc[0].value_counts()) == 0:
        print('DF DATETIME COLUMNS: ', len(df_dt.iloc[0].value_counts()))

None of the other methods (a.any (), a.empty () ...) worked. select returns with a non-empty index, but with empty columns, so I think it is. I think it really returns a series, hence a null character.

0
source

Source: https://habr.com/ru/post/1547726/


All Articles