Home > Software engineering >  Deleting NaN values in list in python
Deleting NaN values in list in python

Time:01-20

num_A=414
for i in range(1,num_A):
 
    interval = data_A.index[data_A['level'] == i]
    datum = data_A.loc[interval] 
    hyumgi_time=np.max(datum.hyumgi)
    print(hyumgi_time)

When I run the code above, hyumgi_time gives some nan values. I think hyumgi_time is a list. I want to make every nan values to 0's. I think this is a list, not a dataframe. So, I wonder how to make nan values to 0's for a list.

CodePudding user response:

look at this snippet:

df['Set_of_Numbers'] = df['Set_of_Numbers'].fillna(0)

for more info - https://www.geeksforgeeks.org/replace-nan-values-with-zeros-in-pandas-dataframe/

CodePudding user response:

Fun fact: NaN is the only value that is not equal to itself. That means you can do this.

no_nan = [x if x==x else 0 for x in hyumgi_time]

CodePudding user response:

basic method if it is a list:

itr = 0
while np.NaN in hyumgi_time:
    if hyumgi_time[itr] == NaN:
        hyumgi_time[itr] = 0

CodePudding user response:

I think you can use list comprehension.

none_converted = [x if x!=None else 0 for x in a]

take a look at it here

  •  Tags:  
  • Related