I am working with a data set from a csv file.
I import the data into a dictionary with this dic = df.to_dict()
This works well but because of the way the data is structured I get a dictionary of dictionaries. The nested dictionary has multiple "nan" values. I need to remove all nan values and the dictionaries can remain nested or I could use a normal dictionary.
The data prints from the dictionary in this format:
{'1/13/2018': {0: 'Monday', 1: 'Red', 2: 'Violet', 3: 'Aqua', 4: 'Pink', 5: 'White', 6: nan, 7: nan, 8: nan},
Here is a sample of my code:
df = pd.read_csv(infile, parse_dates=True, infer_datetime_format=True)
dic = df.to_dict()
I have tried the advice here and attempted to do this with some comprehension but I think because of the nested nature I am not sure how to adapt it.
I have also tried looping in this way:
value_list = []
key_list = []
for k, v in dic.items():
key_list.append(k)
for c, q in v.items():
if str(q) != 'nan':
value_list.append(q)
else:
pass
I was hoping with this I could create a new dic from the two lists. However there is data blurs together and it becomes hard to separate value sets. There must be a better more pythonic way to do this.