This is my first post and I have some problems with my code.
I need to transform my objects list:
mylist=[
[length, 1322, width, 850, high, 620, type, sedan, e, 55, f, 44],
[length, 1400, width, 922, high, 650, type, truck, e, 85, f, 50]
]
Into a dictionary like this:
mydic = {
sedan : {length : 1322, width : 850, high : 620, type : sedan, e : 55, f : 44},
truck : {length : 1400, width : 922, high : 650, type : truck, e : 85, f : 50}
}
I do not know how to do it...
Thanks in Advance!
lista=[["length", 1322, "width", 850, "high", 620, "type", "sedan", "e", 55, "f", 44], ["length", 1400, "width", 922, "high", 650, "type", "truck", "e", 85, "f", 50]]
b=[{q:i[w*2+1] for w,q in enumerate(i[::2])} for i in lista] # Goes through the list and put the key and keyvalue together in the future nested dict
c={i["type"]:i for i in b} #creates the out dict now that it can call for the type of the dict, and assign type to dict
it is slightly ineficient if you list gets bigger but it is a solution.
The output for c btw. is:
{'sedan': {'length': 1322, 'width': 850, 'high': 620, 'type': 'sedan', 'e': 55, 'f': 44}, 'truck': {'length': 1400, 'width': 922, 'high': 650, 'type': 'truck', 'e': 85, 'f': 50}}
i also took the liberty to turn your variables in to strings. Assumed you forgot to put them on :)
First we make the key/value pairs, then we turn those into dictionaries. Then you can use the type entry of those dictionaries to put them in the nested dictionary
def pairs(seq):
it = iter(seq)
return zip(it, it)
mylist=[['length', 1322, 'width', 850, 'high', 620, 'type', 'sedan', 'e', 55, 'f', 44], ['length', 1400, 'width', 922, 'high', 650, 'type', 'truck', 'e', 85, 'f', 50]]
dicts = map(dict, map(pairs, mylist))
result = {d['type']: d for d in dicts}
print(result)
# {'sedan': {'length': 1322, 'width': 850, 'high': 620, 'type': 'sedan', 'e': 55, 'f': 44}, 'truck': {'length': 1400, 'width': 922, 'high': 650, 'type': 'truck', 'e': 85, 'f': 50}}
Related
I have 3 data frames. I need to enrich the data from df with the data columns from df2 and df3 so that df ends up with the columns 'Code', 'Quantity', 'Payment', 'Date', 'Name', 'Size', 'Product','product_id', 'Sector'.
The codes that are in df and not in df2 OR df3, need to receive "unknown" for the string columns and "0" for the numeric dtype columns
import pandas as pd
data = {'Code': [356, 177, 395, 879, 952, 999],
'Quantity': [20, 21, 19, 18, 15, 10],
'Payment': [173.78, 253.79, 158.99, 400, 500, 500],
'Date': ['2022-06-01', '2022-09-01','2022-08-01','2022-07-03', '2022-06-09', '2022-06-09']
}
df = pd.DataFrame(data)
df['Date']= pd.to_datetime(df['Date'])
data2 = {'Code': [356, 177, 395, 893, 697, 689, 687],
'Name': ['John', 'Mary', 'Ann', 'Mike', 'Bill', 'Joana', 'Linda'],
'Product': ['RRR', 'RRT', 'NGF', 'TRA', 'FRT', 'RTW', 'POU'],
'product_id': [189, 188, 16, 36, 59, 75, 55],
'Size': [1, 1, 3, 4, 5, 4, 7],
}
df2 = pd.DataFrame(data2)
data3 = {'Code': [879, 356, 389, 395, 893, 697, 689, 978],
'Name': ['Mark', 'John', 'Marry', 'Ann', 'Mike', 'Bill', 'Joana', 'James'],
'Product': ['TTT', 'RRR', 'RRT', 'NGF', 'TRA', 'FRT', 'RTW', 'DTS'],
'product_id': [988, 189, 188, 16, 36, 59, 75, 66],
'Sector': ['rt' , 'dx', 'sx', 'da', 'sa','sd','ld', 'pc'],
}
df3 = pd.DataFrame(data3)
I was using the following code to obtain the unknown codes by comparing with df2, but now i have to compare with df3 also and also add the data from the columns ['Name', 'Size', 'Product','product_id', 'Sector'].
common = df2.merge(df,on=['Code'])
new_codes = df[(~df['Code'].isin(common['Code']))]
This is my first data frame (df1).
I have to reorder the elements in sequence column (of my df1) based on the minimum count from the second data frame (df2)
so my end results should be like this
I think the code below would do what you want. I've commented inline and put links for further reading...
I know it could be compressed into shorter code, but I wanted the steps to be clear.
import pandas as pd
from pprint import pprint
data1 = {'id': ['A1234', 'A2345'],
'Sequence': ['16 31 17', '51 59 43']}
df1 = pd.DataFrame(data1)
# I assumed the label en count columns are integers
data2 = {'label': [10, 11, 12, 13, 16, 17, 21, 24, 31, 43, 44, 51, 59, 60],
'count': [214, 128, 135, 37, 184, 68, 267, 264, 231, 13, 82, 100, 99, 92]}
df2 = pd.DataFrame(data2)
def seq_value_sort(seq_df, label_df):
new_sequence_list = []
for value in seq_df['Sequence'].values:
print(f'{"":-<40}') # prints a line
# convert string to list of integers
# https://www.geeksforgeeks.org/python-converting-all-strings-in-list-to-integers/
sequence = [int(i) for i in value.split()]
# generate an unsorted list of dict items based on Sequence
data = []
for index, row in label_df.T.iteritems():
if int(row['label']) in sequence:
data.append({'label': int(row['label']),
'count': int(row['count'])})
pprint(data)
# now sort the unsorted list based on key 'count'
# https://stackoverflow.com/a/73050/9267296
data = sorted(data, key=lambda k: k['count'])
pprint(data)
# list comprehension to make list of strings out
# of the list of dict
# https://stackoverflow.com/a/7271523/9267296
sequence_sorted = [ str(item['label']) for item in data ]
pprint(sequence_sorted)
# create the final sequence string from the list
new_sequence_list.append(' '.join(sequence_sorted))
# create return data
return_data = {'id': list(seq_df['id'].values),
'Sequence': new_sequence_list}
pprint(return_data)
# finally return a new df
return pd.DataFrame(return_data)
df3 = seq_value_sort(df1, df2)
print(f'{"":-<40}')
print(df3)
EDIT:
Forgot the output:
----------------------------------------
[{'count': 184, 'label': 16},
{'count': 68, 'label': 17},
{'count': 231, 'label': 31}]
[{'count': 68, 'label': 17},
{'count': 184, 'label': 16},
{'count': 231, 'label': 31}]
['17', '16', '31']
----------------------------------------
[{'count': 13, 'label': 43},
{'count': 100, 'label': 51},
{'count': 99, 'label': 59}]
[{'count': 13, 'label': 43},
{'count': 99, 'label': 59},
{'count': 100, 'label': 51}]
['43', '59', '51']
{'Sequence': ['17 16 31', '43 59 51'], 'id': ['A1234', 'A2345']}
----------------------------------------
id Sequence
0 A1234 17 16 31
1 A2345 43 59 51
When trying to assign Grades to Test_Scores (which when the average is calculated), only returns two assigned dictionaries. For some reason, the last one won't show up. How would I get the third dictionary to show up?
Test_Scores = [{'test_1': 90, 'test_2': 80, 'test_3': 95},
{'test_1': 92, 'test_2': 75, 'test_3': 80},
{'test_1': 80, 'feature_2': 81, 'test_3': 82}]
Grades = ['A', 'B', 'B']
Empty_dict = {}
Empty_dict = dict(zip(Grades, Test_Scores))
print(Empty_dict)
I expect something like:
{'A': {'test_1': 90, 'test_2': 80, 'test_3': 95}, 'B': {'test_1': 92,
'test_2': 75, 'test_3': 80}, {'test_1': 80, 'feature_2': 81, 'test_3':
82}})
to demonstrate the average of the three tests is that assigned key.
However, I get:
{'A': {'test_1': 90, 'test_2': 80, 'test_3': 95}, 'B': {'test_1': 80,
'feature_2': 81, 'test_3': 82}}
By doing dict(zip(Grades, Test_Scores)) you override the value of B with the last occurrence, instead you could do:
Empty_dict = {}
for score, grade in zip(Test_Scores, Grades):
Empty_dict.setdefault(grade, []).append(score)
print(Empty_dict)
Output
{'A': [{'test_1': 90, 'test_2': 80, 'test_3': 95}], 'B': [{'test_1': 92, 'test_2': 75, 'test_3': 80}, {'test_1': 80, 'feature_2': 81, 'test_3': 82}]}
I have a python dictionary in this format:
{('first', 'negative'): 57, ('first', 'neutral'): 366, ('first', 'positive'): 249, ('second', 'negative'): 72, ('second', 'neutral'): 158, ('second', 'positive'): 99, ('third', 'negative'): 156, ('third', 'neutral'): 348, ('third', 'positive'): 270}
I want to convert it to:
{'first': [{'sentiment':'negative', 'value': 57}, {'sentiment': 'neutral', 'value': 366}, {'sentiment': 'positive', 'value': 249}], 'second': [{'sentiment':'negative', 'value': 72}, {'sentiment': 'neutral', 'value': 158}, {'sentiment': 'positive', 'value': 99}], 'third': [{'sentiment':'negative', 'value': 156}, {'sentiment': 'neutral', 'value': 348}, {'sentiment': 'positive', 'value': 270}]}
Thanks in advance
This should help.
o = {('first', 'negative'): 57, ('first', 'neutral'): 366, ('first', 'positive'): 249, ('second', 'negative'): 72, ('second', 'neutral'): 158, ('second', 'positive'): 99, ('third', 'negative'): 156, ('third', 'neutral'): 348, ('third', 'positive'): 270}
d = {}
for k,v in o.items(): #Iterate over your dict
if k[0] not in d:
d[k[0]] = [{"sentiment":k[1] , "value": v}]
else:
d[k[0]].append({"sentiment":k[1] , "value": v})
print d
Output:
{'second': [{'value': 72, 'sentiment': 'negative'}, {'value': 99, 'sentiment': 'positive'}, {'value': 158, 'sentiment': 'neutral'}], 'third': [{'value': 156, 'sentiment': 'negative'}, {'value': 348, 'sentiment': 'neutral'}, {'value': 270, 'sentiment': 'positive'}], 'first': [{'value': 57, 'sentiment': 'negative'}, {'value': 366, 'sentiment': 'neutral'}, {'value': 249, 'sentiment': 'positive'}]}
from collections import defaultdict
out = defaultdict(list)
for (label, sentiment), value in input_dict.items():
out[label].append(dict(sentiment=sentiment, value=value))
I'm trying to use a for loop to generate random values for item prices, by changing the value of the item prices in a pre-defined dictionary.
The new values of this pre-defined dictionary are then added to the end of another pre-defined dictionary so a history of prices can be stored.
here is my code:
tradable_good_prices= {'iron' : 0, 'gold' : 0, 'lead' : 0, 'ruby' : 0 'emerald' : 0, 'steel' : 0, 'diamond' : 0}
item_list = tradable_good_prices.keys()
item_price_history = {}
def Random_economy(generations):
for generation_number in range(0, generations):
for list_number in range(0, len(item_list)):
tradable_good_prices[item_list[list_number]] = np.random.random_integers(100,1000)
print(tradable_good_prices)
item_price_history[generation_number] = tradable_good_prices
print(item_price_history)
Random_economy(2)
the code takes in generations as an argument for the number of for loop iterations. And using a value of 2 for generations this output is produced on the console:
{'steel': 821, 'diamond': 477, 'lead': 325, 'gold': 914, 'iron': 542, 'emerald': 360, 'ruby': 705}
{0: {'steel': 821, 'diamond': 477, 'lead': 325, 'gold': 914, 'iron': 542, 'emerald': 360, 'ruby': 705}}
{'steel': 751, 'diamond': 158, 'lead': 322, 'gold': 662, 'iron': 180, 'emerald': 846, 'ruby': 570}
{0: {'steel': 751, 'diamond': 158, 'lead': 322, 'gold': 662, 'iron': 180, 'emerald': 846, 'ruby': 570}, 1: {'steel': 751, 'diamond': 158, 'lead': 322, 'gold': 662, 'iron': 180, 'emerald': 846, 'ruby': 570}}
As can be seen the previous values are being overwritten, I'm guessing theres quite a simple explanation for this like "the dictionary storing the different generation values is referencing the first one for its values" but I cannot find help on this matter anywhere.
So could someone please explain to me what I'm doing wrong.
The keys in a dictionary are unique. If a key exists in a dictionary, d[key] = other_value just changes the value for that key, it does NOT create another item.
>>> d = {'a':1, 'b':'foo'}
>>> d['b'] = 'six'
>>> d
{'b': 'six', 'a': 1}
>>> d.update([('a','bar')])
>>> d
{'b': 'six', 'a': 'bar'}
>>>
If you have data that you want to place in a dictionary and the data contains keys with multiple values, you could put the values into a list for each key. collections.defaultdict makes this easy.
>>> a
[('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4), ('f', 5), ('a', 100), ('c', 99), ('d', 98), ('f', 97)]
>>> import collections
>>> d = collections.defaultdict(list)
>>> for key, value in a:
d[key].append(value)
>>> d
defaultdict(<class 'list'>, {'b': [1], 'a': [0, 100], 'e': [4], 'f': [5, 97], 'd': [3, 98], 'c': [2, 99]})
>>>
For your problem, start with the initial values in a list then add to them.
import random
d = {'a':[0], 'b':[0], 'c':[0]}
for _ in xrange(4):
for key in d:
d[key].append(random.randint(1, 100))
for item in d.items():
print item
>>>
('a', [0, 92, 45, 52, 32])
('c', [0, 51, 85, 72, 4])
('b', [0, 47, 7, 74, 59])
>>>
How to iterate over a dictionary.