Python:从字符串数组中删除 np nan 值

标签 python pandas

如何从字符串数组中删除 np nan 值?

我创建了一个 LIST,我在其中附加了不存在于基于另一个 DF 的 DF 中的值。

例如:

NotPresent = [Alan, Susie, nan, nan, nan] 

我希望能够这样转换列表:

NotPresent = [Alan, Susie] 

在 Python 中处理此问题的最简单方法是什么?

谢谢

最佳答案

如果你的意思是 pandas DataFrame 那么你可以使用 dropna()

>>> import pandas as pd
>>> import numpy as np
>>> data = [['Alan'], ['Suzie'], [np.nan], [np.nan]]
>>> df = pd.DataFrame(data, columns=['col1'])
>>> df
    col1
0   Alan
1  Suzie
2    NaN
3    NaN
>>> df.dropna()
    col1
0   Alan
1  Suzie

其他人已经回答了他们在列表中处理 nan 的版本。这是使用 numpy

中的 nan 的另一种方法
>>> sample
['Alan', 'Suzie', nan, nan]
>>> from numpy import nan
>>> [x for x in sample if x is not nan]
['Alan', 'Suzie']

关于Python:从字符串数组中删除 np nan 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48934288/

相关文章:

python - 打印 PyTorch 张量的精确值(浮点精度)

python word2vec 使用周围单词的上下文相似度

python - 使用 python 将多个 JSON 文件插入 MongoDB

pandas - 按级别子图多索引数据

python - 如何 reshape 这个数据框

python - 两个发行版可以交付相同的包裹吗?

python - 捕获的 Python 异常的更改消息(至少在 Python 3.x 上,希望在 2.5+ 上也是如此)

python Pandas : Merge two tables without keys (Multiply 2 dataframes with broadcasting all elements; NxN dataframe)

python - Pandas to_datetime 解析错误的年份

python - Python 中带键的 sort_values()