Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从dictionary-Python中删除nan_Python_Pandas - Fatal编程技术网

从dictionary-Python中删除nan

从dictionary-Python中删除nan,python,pandas,Python,Pandas,我想从我的字典中删除“南” my_dict = {'House': ['has keys', 'check lights', nan, nan, nan], 'The Office': ['reading', nan, nan, nan, 'coffee breaks']} 我相信南是浮标,不是弦。我试过: import math my_dict['House'] = [ x for x in dict2['House'] if no

我想从我的字典中删除“南”

my_dict = {'House': ['has keys',
  'check lights',
  nan,
  nan,
  nan],
 'The Office': ['reading',
  nan,
  nan,
  nan,
  'coffee breaks']}
我相信南是浮标,不是弦。我试过:

import math

my_dict['House'] = [
    x
    for x in dict2['House']
    if not (isinstance(x, float) and math.isnan(x))
]
我得到:

my_dict = {'House': ['has keys',
  'check lights',],
 'The Office': ['reading',
  nan,
  nan,
  nan,
  'coffee breaks']}
我希望它看起来像下面这样,但我不知道如何让我的for循环遍历所有钥匙,而不仅仅是House:

my_dict = {'House': ['has keys',
  'check lights'],
 'The Office': ['reading',
  'coffee breaks']}

这应该可以工作,它将过滤字典中的所有值,删除NaN编号:

{ k: [x for x in v if not isinstance(x, float) or not math.isnan(x)] for k, v in my_dict.items() }
结果是:

{'House': ['has keys', 'check lights'],
 'The Office': ['reading', 'coffee breaks']}

这应该可以工作,它将过滤字典中的所有值,删除NaN编号:

{ k: [x for x in v if not isinstance(x, float) or not math.isnan(x)] for k, v in my_dict.items() }
结果是:

{'House': ['has keys', 'check lights'],
 'The Office': ['reading', 'coffee breaks']}

您在正确的轨道上,但您只检查“house”,您需要将您的逻辑应用于所有钥匙:

import math
for tv_show in my_dict:
    my_dict[tv_show] = [
        x
        for x in dict2[tv_show]
        if not (isinstance(x, float) and math.isnan(x))
    ]

您在正确的轨道上,但您只检查“house”,您需要将您的逻辑应用于所有钥匙:

import math
for tv_show in my_dict:
    my_dict[tv_show] = [
        x
        for x in dict2[tv_show]
        if not (isinstance(x, float) and math.isnan(x))
    ]

您也可以反过来只保留字符串值(当然,只要您只需要字符串值):

即使这也可以在a中实现,我认为在这种情况下,对于单行程序,逻辑有点密集,但是,它看起来是这样的:

>>> {k: [val for val in v if isinstance(val, str)] for k, v in my_dict.items()}

您也可以反过来只保留字符串值(当然,只要您只需要字符串值):

即使这也可以在a中实现,我认为在这种情况下,对于单行程序,逻辑有点密集,但是,它看起来是这样的:

>>> {k: [val for val in v if isinstance(val, str)] for k, v in my_dict.items()}

由于您已标记了熊猫,因此可以执行以下操作:

print(df)




由于您已标记了熊猫,因此可以执行以下操作:

print(df)




这里的
pandas
标记的用途是什么?此数据是否存储在熊猫数据框中?@Erfan是的,它存储在熊猫数据框中。如果与熊猫无关,我可以尝试移除标签!这里的
pandas
标记的用途是什么?此数据是否存储在熊猫数据框中?@Erfan是的,它存储在熊猫数据框中。如果与熊猫无关,我可以尝试移除标签!