简体   繁体   English

如何删除字符串值列表中的“nan”值?

[英]How to remove 'nan' values in a list of string values?

I have a list, made of values from various rows, those values have to be converted into strings.我有一个列表,由来自不同行的值组成,这些值必须转换为字符串。 The problem is when a row is empty there is a value called 'nan' displayed, which I would like to remove.问题是当一行为空时,会显示一个名为“nan”的值,我想将其删除。

My code:我的代码:

import pandas as pd

test = []
for index, row in df.iterrows():
x = str(row['Date']) + ' | ' + str(row['Time'])
test.append(x)

print(test)

I tried multiple things :我尝试了多种方法

import numpy as np

list_clean = test[np.logical_not(np.isnan(test))]
print(res_backend_nan)

Which says哪个说

TypeError: only integer scalar arrays can be converted to a scalar index TypeError: 只有 integer 标量 arrays 可以转换为标量索引

I tried:我试过了:

import math
from numpy import nan

list_clean = [item for item in test if not(math.isnan(item) == False)]

And it says:它说:

TypeError: must be real number, not P TypeError:必须是实数,而不是 P

I tried:我试过了:

list_clean = [item for item in list if not(pd.isnull(item) == True)]

The result is the list is still displayed with nan values:结果是列表仍然显示为 nan 值:

06/07/35 | 06/07/35 | nan

Some help would be very welcome please, thank you.一些帮助将非常受欢迎,谢谢。

Try something like the following:尝试以下操作:

import pandas as pd

def remove_nan(row):
    return [str(x) for x in row if not pd.isnull(x)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM