Trouble adding multiple dict k:v pairs - python

I'm not understanding how the k:v coding works. I've read that k:v pairs the items. k is the key and v is the item. If I want an additional field called 'cusip' in addition to 'lastPrice', how would I add that? Thanks
response_dict = response.json()
new_dict = {k: v['lastPrice'] for k, v in response_dict.items()}
df = pd.DataFrame.from_dict(new_dict, orient='index', columns=['lastPrice'])

You just need to build the appropriate tuple.
new_dict = {k: (v['lastPrice'], v['cusIP']) for k, v in response_dict.items()}
In a dictionary comprehension {key_expr: value_expr for ...}, both key_expr and value_expr are allowed to be arbitrary expressions.

Your question is vague, but I would like simulate it maybe useful or close to your issue solution:
Instead of response I defined a new dict with initial values.
import pandas as pd
response_dict = {"price":[10,5,9],"Products":["shoes","clothes","hat"], "lastprices":[5,6,7]}
new_dict = {k: v for k, v in response_dict.items()}
df = pd.DataFrame.from_dict(new_dict)
df
If you keen to add new key with new values just try to modify dictioner: for instance
new_dict["cusip"]=[1,2,3]
df = pd.DataFrame.from_dict(new_dict)
df

Related

Manipulating column names in a multiindex dataframe

I converted the following dictionary to a dataframe:
dic = {'US':{'Traffic':{'new':1415, 'repeat':670}, 'Sales':{'new':67068, 'repeat':105677}},
'UK': {'Traffic':{'new':230, 'repeat':156}, 'Sales':{'new':4568, 'repeat':10738}}}
d1 = defaultdict(dict)
for k, v in dic.items():
for k1, v1 in v.items():
for k2, v2 in v1.items():
d1[(k, k2)].update({k1: v2})
df.insert(loc=2, column=' ', value=None)
df.insert(loc=0, column='Mode', value='Website')
df.columns = df.columns.rename("Metric", level=1)
The dataframe currently looks like:
How do I move the column header - Mode to the following row?
To get an output of this sort:
Change this:
df.insert(loc=0, column='Mode', value='Website')
to this:
df.insert(loc=0, column=('', 'Mode'), value='Website')
then your full code looks like this:
import pandas as pd
from collections import defaultdict
dic = {'US':{'Traffic':{'new':1415, 'repeat':670}, 'Sales':{'new':67068, 'repeat':105677}},
'UK': {'Traffic':{'new':230, 'repeat':156}, 'Sales':{'new':4568, 'repeat':10738}}}
d1 = defaultdict(dict)
for k, v in dic.items():
for k1, v1 in v.items():
for k2, v2 in v1.items():
d1[(k, k2)].update({k1: v2})
df = pd.DataFrame.from_dict(d1)
df.insert(loc=0, column=('', 'Mode'), value='Website')
and this is your df
Rinse and repeat with your empty column between US and UK.
(though, admittedly, this looks like a strange way of handling stuff)

how to merge keys in dictionary outside of list?

I'm trying to get dictionary with same keys and merge its values and if there is a duplicate leave only one value of duplicate.
data = {"test1":["data1", "data2"],
"test1":["data3", "data4", "data2"],
"test2":["1data", "2data"],
"test2":["3data", "4data", "2data"]
}
desired_result = {"test1":["data1", "data2", "data3", "data4"],
"test2":["1data", "2data", "3data", "4data"]
}
any ideas how to get result?
First you need create list of dict (because you can't have dictionary with same keys) then iterate over them and extend them to list with key of dict then use set for delete duplicated like below:
data = [{"test1":["data1", "data2"]},{"test1":["data3", "data4", "data2"]},{"test2":["1data", "2data"]},{"test2":["3data", "4data", "2data"]}]
from collections import defaultdict
rslt_out = defaultdict(list)
for dct in data:
for k,v in dct.items():
rslt_out[k].extend(v)
for k,v in rslt_out.items():
rslt_out[k] = list(set((v)))
print(rslt_out)
output:
defaultdict(list,
{'test1': ['data3', 'data4', 'data2', 'data1'],
'test2': ['2data', '3data', '1data', '4data']})

How to iterate through this nested dictionary within a list using for loop

I have a list of nested dictionaries that I want to get specific values and put into a dictionary like this:
vid = [{'a':{'display':'axe', 'desc':'red'}, 'b':{'confidence':'good'}},
{'a':{'display':'book', 'desc':'blue'}, 'b':{'confidence':'poor'}},
{'a':{'display':'apple', 'desc':'green'}, 'b':{'confidence':'good'}}
]
I saw previous questions similar to this, but I still can't get the values such as 'axe' and 'red'. I would like the new dict to have a 'Description', 'Confidence' and other columns with the values from the nested dict.
I have tried this for loop:
new_dict = {}
for x in range(len(vid)):
for y in vid[x]['a']:
desc = y['desc']
new_dict['Description'] = desc
I got many errors but mostly this error:
TypeError: string indices must be integers
Can someone please help solve how to get the values from the nested dictionary?
You don't need to iterate through the keys in the dictionary (the inner for-loop), just access the value you want.
vid = [{'a':{'display':'axe', 'desc':'red'}, 'b':{'confidence':'good'} },
{'a':{'display':'book', 'desc':'blue'}, 'b':{'confidence':'poor'}},
{'a':{'display':'apple', 'desc':'green'}, 'b':{'confidence':'good'}}
]
new_dict = {}
list_of_dicts = []
for x in range(len(vid)):
desc = vid[x]['a']['desc']
list_of_dicts.append({'desc': desc})
I have found a temporary solution for this. I decided to use the pandas dataframe instead.
df = pd.DataFrame(columns = ['Desc'])
for x in range(len(vid)):
desc = vid[x]['a']['desc']
df.loc[len(df)] = [desc]
so you want to write this to csv later so pandas will help you a lot for this problem using pandas you can get the desc by
import pandas as pd
new_dict = {}
df = pd.DataFrame(vid)
for index, row in df.iterrows() :
new_dict['description'] = row['a']['desc']
a b
0 {'display': 'axe', 'desc': 'red'} {'confidence': 'good'}
1 {'display': 'book', 'desc': 'blue'} {'confidence': 'poor'}
2 {'display': 'apple', 'desc': 'green'} {'confidence': 'good'}
this is how dataframe looks like a b are column of the dataframe and your nested dicts are rows of dataframe
Try using this list comprehension:
d = [{'Description': i['a']['desc'], 'Confidence': i['b']['confidence']} for i in vid]
print(d)

How to combine multiple dictionaries in a list based on the given key columns?

I am working on a List which contains many dictionaries. Here I am trying to combine those dictionary into a single dict based on their key value. For illustration see the below example.
my_dict =[{'COLUMN_NAME': 'TABLE_1_COL_1', 'TABLE_NAME': 'TABLE_1'},
{'COLUMN_NAME': 'TABLE_1_COL_2', 'TABLE_NAME': 'TABLE_1'},
{'COLUMN_NAME': 'TABLE_1_COL_3', 'TABLE_NAME': 'TABLE_1'},
{'COLUMN_NAME': 'TABLE_2_COL_1', 'TABLE_NAME': 'TABLE_2'},
{'COLUMN_NAME': 'TABLE_2_COL_2', 'TABLE_NAME': 'TABLE_2'}]
Here for any key value matches with another key value then need to combine other key values.
Below is the sample output what I expect from the above list of dict.
new_lst = [{'TABLE_NAME': 'TABLE_1','COLUMN_NAME':['TABLE_1_COL_1','TABLE_1_COL_2','TABLE_1_COL_3']}, {'TABLE_NAME': 'TABLE_2','COLUMN_NAME': ['TABLE_2_COL_1','TABLE_2_COL_2']]
How can i achieve this in most efficient way.
You can use defaultdict to get similar output.
from collections import defaultdict
new_lst = []
for some_dict in list_of_dicts:
new_lst.append(defaultdict(list))
for key, value in some_dict.items():
new_lst[len(new_lst) - 1][key].append(value)
new_lst will be of the form:
[{'TABLE_NAME': ['TABLE_1'],'COLUMN_NAME':['TABLE_1_COL_1','TABLE_1_COL_2','TABLE_1_COL_3']}, {'TABLE_NAME': ['TABLE_2'],'COLUMN_NAME': ['TABLE_2_COL_1','TABLE_2_COL_2']]
Which is slightly different from what you wanted (even the singular elements are in arrays). I would recommend you leave it in this format if given the choice.
To get exactly what you wanted, add this after the above code:
for some_dict in new_lst:
for key, value in some_dict.items():
if len(value) == 1:
some_dict[key] = value[0]
Now, new_lst is exactly like you expected:
[{'TABLE_NAME': 'TABLE_1','COLUMN_NAME':['TABLE_1_COL_1','TABLE_1_COL_2','TABLE_1_COL_3']}, {'TABLE_NAME': 'TABLE_2','COLUMN_NAME': ['TABLE_2_COL_1','TABLE_2_COL_2']]
Something like that?
data = {}
for element in my_dict:
table_name = element['TABLE_NAME']
column_name = element['COLUMN_NAME']
if table_name not in data:
data[table_name] = []
data[table_name].append(column_name)
new_lst = [{'TABLE_NAME': key, 'COLUMN_NAME': val} for key, val in data.items()]

Sorting a list with dictionaries, django

myFrom two models in django i have created a list of dictionarys where each dictonary is a row in a table I show in the client.
I would like to be able to sort this list for each of the different "columns".
objdict = []
mydict = {
'thing1': model1.val1,
'thing2': model2.val1,
'thing3': model2.val2,
'thing4': model1.val2,
'thing5': model1.val3,
}
objdict.append(mydict)
Say i would like to sort this list on thing1 in ascending order.
How could I be able to acheive this?
You should just be able to do:
sorted_objects = sorted(objdict, key=lambda k: k['thing1'])
will get you ascending order. For descending:
sorted_objects = sorted(objdict, key=lambda k: k['thing1'], reverse=True)

Categories