gpt4 book ai didi

python - 有没有办法从充满数据的字典中删除 nan ?

转载 作者:太空狗 更新时间:2023-10-30 00:22:52 27 4
gpt4 key购买 nike

我有一个字典,里面装满了我导入的两个文件的数据,但有些数据以 nan 格式输出。如何使用 nan 删除数据片段?

我的代码是:

import matplotlib.pyplot as plt 
from pandas.lib import Timestamp
import numpy as np
from datetime import datetime
import pandas as pd
import collections

orangebook = pd.read_csv('C:\Users\WEGWEIS_JAKE\Desktop\Work Programs\Code Files\products2.txt',sep='~', parse_dates=['Approval_Date'])
specificdrugs=pd.read_csv('C:\Users\WEGWEIS_JAKE\Desktop\Work Programs\Code Files\Drugs.txt',sep=',')

"""This is a dictionary that collects data from the .txt file
This dictionary has a key,value pair for every generic name with its corresponding approval date """
drugdict={}
for d in specificdrugs['Generic Name']:
drugdict.dropna()
drugdict[d]=orangebook[orangebook.Ingredient==d.upper()]['Approval_Date'].min()

我应该从这段代码中添加或删除什么以确保字典中没有值为 nan 的键值对?

最佳答案

from math import isnan

如果 nans 被存储为键:

# functional
clean_dict = filter(lambda k: not isnan(k), my_dict)

# dict comprehension
clean_dict = {k: my_dict[k] for k in my_dict if not isnan(k)}

如果 nans 被存储为值:

# functional
clean_dict = filter(lambda k: not isnan(my_dict[k]), my_dict)

# dict comprehension
clean_dict = {k: my_dict[k] for k in my_dict if not isnan(my_dict[k])}

关于python - 有没有办法从充满数据的字典中删除 nan ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24068306/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com