'How to check if a Pandas DataFrame has enough datas?

My first post ever since I can't find a solution anywhere :)

I'm trying to check if a Pandas DataFrame has enough datas.

For example:

  • I import 420 rows and I want to get a specific column at row number 418 and 419 (the two last rows imported)

If I have enough datas, all went smooth but if I have less than 420 rows, then I get errors...

I fixed it first by using df.iloc[-1] instead of df.loc[419] or df.iloc[-2] for df.loc[418], so I'm getting the last rows even if there is less than 420 (which is not the best..) but when there is only 1 row, then I get this error:

IndexError: single positional indexer is out-of-bounds

Here is my code:

df = pd.DataFrame(historical['result'])
df = df.fillna(0)

# Volume Weekly
VW = 420
df['volumew'] = df['volume'].rolling(VW).mean()
 
print(df.iloc[-2]['volumew'])

It might make senses somewhere but what am I doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source