PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 28, 2022

[FIXED] How to check whether a DataFrame is empty?

 October 28, 2022     dataframe, is-empty, pandas, python, python-3.x     No comments   

Issue

I want to check whether a DataFrame is empty :

    BTC_ewma_24  ETH_ewma_24  DASH_ewma_24
24  4011.235578   334.597119        281.15
25  4011.285662   334.591056        281.15
26  4011.373673   334.603479        281.15
27  4011.453068   334.614686        281.15
28  4011.526571   334.624813        281.15
29  4011.591356   334.633980        281.15
30  4011.650075   334.642288        281.15
31  4011.703366   334.649828        281.15

I tried if(self.mean_exp.bool() == False): but it answers me :

ValueError: The truth value of a DataFrame is ambiguous.
Use a.empty, a.bool(), a.item(), a.any() or a.all().

As if it didn't even noticed that I used .bool()

I then used a.empty and it answered me :

AttributeError: 'list' object has no attribute 'empty'

Solution

IIUC: there is .empty attribute:

DataFrame:

In [86]: pd.DataFrame().empty
Out[86]: True

In [87]: pd.DataFrame([1,2,3]).empty
Out[87]: False

Series:

In [88]: pd.Series().empty
Out[88]: True

In [89]: pd.Series([1,2,3]).empty
Out[89]: False

NOTE: checking the length of DF (len(df)) might save you a few milliseconds compared to df.empty method ;-)

In [142]: df = pd.DataFrame()

In [143]: %timeit df.empty
8.25 µs ± 22.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [144]: %timeit len(df)
2.35 µs ± 7.56 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [145]: df = pd.DataFrame(np.random.randn(10*5, 3), columns=['a', 'b', 'c'])

In [146]: %timeit df.empty
15.3 µs ± 269 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [147]: %timeit len(df)
3.58 µs ± 12.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)


Answered By - MaxU - stop genocide of UA
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing