Home > other >  print value if it is not nan
print value if it is not nan

Time:12-28

I have a df with a column containing floats (transaction values). I would liek to iterate trough the column and only print the value if it is not nan. Right now i have the following condition.

if j > 0:
  print(j)
  i  = 1
else: i  = 1

where i is my iteration number.

I do this because I know that in my dataset there are no negative values and that is my workaound, but I would like to know how it would be done correctly if I would have nagative values. so what would be the if condition ?

I have tried j != None and j != np.nan but it still prints all nan.

CodePudding user response:

Why not use built-in pandas functionality?

Given some dataframe:

import pandas as pd
import numpy as np
df = pd.DataFrame({
    'a': [-1, -2, 0, np.nan, 5, 6, np.nan]
})

You can filter out all nans:

df[df['a'].notna()]
>>> a
0   -1.0
1   -2.0
2   0.0
4   5.0
5   6.0

or only positive numbers:

df[df['a']> 0]
>>> a
4   5.0
5   6.0

CodePudding user response:

Suppose let's assume here the datatype is int64 for the filled values in column

for i in range(0,10):
    if(type(array[i])!= int):
        print(array[i])
  • Related