Storing multiple values into Dictionary - excel

I am new to python and am facing difficulties in dictionary. I am trying to store multiple values from excel into dictionary.
This is my input:
And i am trying to store in this way.
My expected output is:
d = [
{
"name":"dhdn",
"sub":["c","java","python"]
},
{
"name":"subbu",
"sub":["java","perl"]
}
]
I tried like this:
df_service = pd.read_excel(existing_excel_file, sheet_name='Sheet1')
df_service = df_service.replace(np.nan, "dummy")
print(df_service)
list1 = []
for i in range(0, len(df_service['name'])):
dict1 = {}
lst = []
if df_service['name'][i] != 'dummy':
dict1["name"] = df_service['name'][i]
lst.append(df_service['sub'][i])
else:
lst.append(df_service['sub'][i])
dict1["sub"] = lst
list1.append(dict1)
print(list1)
And what if the excel data is like given below:
What if we have data like this? How to create a dictionary for this?
Need some suggestion, not getting any idea.

df_service = df_service.fillna(method='ffill')
result = [{'name':k[0],'usn':k[1],'sub':v["sub"].tolist(),"marks":v["marks"].tolist()} for k,v in df_service.groupby(['name', 'usn'])]
pprint (result)

You can use pandas.DataFrame.fillna with the method='ffill' option. 'ffill' stands for 'forward fill' and will propagate last valid observation forward.
df_service = pd.read_excel(existing_excel_file, sheet_name='Sheet1')
df_service = df_service.fillna(method='ffill')
result = [{'name':k,'sub':g["sub"].tolist()} for k,g in df_service.groupby("name")]
print (result)
output:
[{'name': 'dhdn', 'sub': ['c', 'java', 'python']}, {'name': 'subbu', 'sub': ['java', 'perl']}]

Hi #ncica, I appreciate your answer, what if we have data like this? How to create a dictionary for this.

Related

convert list of dictionary values in to list of values

Hi all below is my list of dictionary
a=[{'Name': 'dhaya', 'Place': 'pune', 'Designation': 'fleetEngineer'},
{'Name': 'rishi', 'Place': 'maharastra', 'Designation': 'Sr.Manager'}]
iam expecting output like this
a={"Name":["dhaya","rishi],"Place":["pune","maharastra"],Designation:["fleetEngineer","Sr.Manager"]
"}
can any one assist
new_dict is the answer to the question you posted. Its the smallest and simplest.
b = list(a[0].keys())
new_dict = {}
for x in b:
new_dict[x] = [l[x] for l in a]
print('My expected dictionary',new_dict)
One can use nested for loops for this:
inputList = [{'Name': 'dhaya', 'Place': 'pune', 'Designation': 'fleetEngineer'}, {
'Name': 'rishi', 'Place': 'maharastra', 'Designation': 'Sr.Manager'}]
outputDict = {}
for origDict in inputList:
for key, val in origDict.items():
if key in outputDict:
outputDict[key].append(val)
else:
outputDict[key] = [val]
print(outputDict)
a=[{'Name': 'dhaya', 'Place': 'pune', 'Designation': 'fleetEngineer'}, {'Name': 'rishi', 'Place': 'maharastra', 'Designation': 'Sr.Manager'}]
Name = []
Place = []
Designation = []
for ele in a:
Name.append(ele["Name"])
Place.append(ele["Place"])
Designation.append(ele["Designation"])
new_a = {}
new_a['Name'] = Name
new_a['Place'] = Place
new_a["pune"] = Designation
print(new_a)

Adding keys and value pair to a dictionary within a dictionary with duplicate keys

results is a list within a list with the data as shown in the results section. I am hoping to achieve a dictionary within a dictionary as shown in the results portion.
input:
results = [['abc','12'3,'1123','qwe', 'asd'],['abc','123,'1123','qwe', '123'],['abc','123','1123','ewq','zxc'], ['bcd','123','1123','ewq','zxc'], ['bcd','123','1123','ewq','zxc]]
Code:
report_dict = dict()
axis_list = []
results = self.report_data(conn)
for row in results:
try:
report_dict[row[0]] = {}
report_dict[row[0]][row[3]] = row[1]
except IndexError:
None
print(report_dict)
Result:
report_dict = { 'abc': {'qwe':['asd','123'], 'ewq':['zxc']}, 'bcd' : {'qwe':['asd'], 'ewq':['zxc']} …..}
Please note there are duplicate keys in the dataset.
you could do:
d = {}
for i in results:
if not d.get(i[0],0):
d[i[0]] = {}
if not d[i[0]].get(i[3],0):
d[i[0]][i[3]] = []
d[i[0]][i[3]].append(i[4])
d
{'abc': {'qwe': ['asd', '123'], 'ewq': ['zxc']}, 'bcd': {'ewq': ['zxc', 'zxc']}}
The following is a solution to your immediate question with tuples in the list:
from collections import defaultdict
report_dict = defaultdict(list)
# results = self.report_data(conn)
results = [["abc",123,1123,"qwe", "asd"],["abc",123,1123,"ewq","zxc"], ["bcd",123,1123,"ewq","zxc"], ["bcd",123,1123,"ewq","zxc"]]
for row in results:
try:
report_dict[row[0]].append((row[3], row[1]))
except IndexError:
None
print(report_dict)
Result: defaultdict(<class 'list'>, {'abc': [('qwe', 123), ('ewq', 123)], 'bcd': [('ewq', 123), ('ewq', 123)]})
you can also change it to a dictionaries in the list with the following line
report_dict[row[0]].append({row[3]: row[1]})
Result: defaultdict(<class 'list'>, {'abc': [{'qwe': 123}, {'ewq': 123}], 'bcd': [{'ewq': 123}, {'ewq': 123}]})

How do you create a new dictionary from a list, using positions to dictate keys and values?

I am trying to determine the most used active users in certain packages. I have a list with the packages and users as the elements. I would like to turn these into dictionary with the package name as the key and the username as the value. The pattern of the list is :
list = ['package1', 'userA', 'userB', 'package2', 'userC',
'userD', 'package3', 'userE', 'userF', ...]
I would like:
dict = {'package1': ['userA', 'userB'],
'package2': ['userC', 'userD'],
'package3': ['userE', 'userF'],
...}
I would like to be able to match the package by name and not by position. I currently have something like:
dict={}
for x in list:
if "package" in x:
dict.fromkeys(list, x)
Thanks for your help.
This is one approach using a simple iteration.
Ex:
lst = ['package1', 'userA', 'userB', 'package2', 'userC', 'userD', 'package3', 'userE', 'userF']
result = []
for i in lst:
if i.startswith("package"):
result.append([i])
else:
result[-1].append(i)
result = {i: v for i, *v in result}
print(result)
Output:
{'package1': ['userA', 'userB'],
'package2': ['userC', 'userD'],
'package3': ['userE', 'userF']}
Check out this code;
In [1]: raw_data = ['package1', 'userA', 'userB', 'package2', 'userC', 'userD',
...: 'package3', 'userE', 'userF']
In [2]: data = {}
In [3]: for el in raw_data:
...: if el.startswith('package'):
...: lastkey = el
...: data[el] = []
...: else:
...: data[lastkey].append(el)
...:
In [4]: data
Out[4]:
{'package1': ['userA', 'userB'],
'package2': ['userC', 'userD'],
'package3': ['userE', 'userF']}
If you can rely upon there always being exactly two users, then you ca easily use a dictionary comprehension with indexing and slicing to produce the keys and value-lists that you need:
output = {lst[i]: lst[i+1:i+3] for i in range(0, len(lst), 3)}
If the number of users might be variable though, you'll need a full loop. You need to remember the most recent package you've seen, and put each subsequent user into its associated list:
output = {}
current_package = unpackaged_users = []
for item in lst:
if item.startswith('package'):
current_package = []
output[item] = current_package
else: # or elif item.startswith('user')?
current_package.append(item)
The unpackaged_users variable will contain a list of any users that were included in the input before the first package was seen. If that can't happen in your code, you could probably do away with that variable (and if you don't initialize current_package before the loop, you'll get an exception if that situation ever comes up).

Transforming list into dictionary with Python

There is a list of values:
list_inside = ["de:14612:1","de:14612:21","de:14612:17","de:14612:16"]
Why instead of {'from': '1', 'to': '16'} I am getting this {'from': '16', 'to': '16'}. What I am doing wrong with my code?
keys = ["from", "to"]
list_inside = ["de:14612:1","de:14612:21","de:14612:17","de:14612:16"]
for i in list_inside[::len(list_inside)-1]:
result = dict((key, i.join(i.split(":", 2)[2::1])) for key in keys)
print(result)
Use
keys = ["from", "to"]
list_inside = ["de:14612:1","de:14612:21","de:14612:17","de:14612:16"]
result = {} #Declare empty dict
for key, v in zip(keys, list_inside[::len(list_inside)-1]):
result.update({key: v.split(":", 2)[-1])}) #Use dict.update to update the required values.
print(result)
Output:
{'to': '16', 'from': '1'}
Your current approach is overriding result variable.
Your doing result = several times inside a loop.
Obviously, only the last iteration will be effective here.
This code gets the result what you are expecting.
-1 made simple code.
keys = ["from", "to"]
list_inside = ["de:14612:1","de:14612:21","de:14612:17","de:14612:16"]
result = {}
result[keys[0]] = list_inside[0].split(':')[2]
result[keys[1]] = list_inside[-1].split(':')[2]
print (result)

How to create a dictionary from CSV using a loop function?

I am trying to create several dictionaries out of a table of comments from a CSV with the following columns:
I need to create a dictionary for every row (hopefully using a loop so I don't have to create them all manually), where the dictionary keys are:
ID
ReviewType
Comment
However, I cannot figure out a fast way to do this. I tried creating a list of dictionaries using the following code:
# Import libraries
import csv
import json
import pprint
# Open file
reader = csv.DictReader(open('Comments.csv', 'rU'))
# Create list of dictionaries
dict_list = []
for line in reader:
dict_list.append(line)
pprint.pprint(dict_list)
However, now I do not know how to access the dictionaries or whether the key value pairs are matched properly since in the following image:
The ID, ReviewType and Comment do not seem to be showing as
dictionary keys
The Comment value seems to be showing as a list of half-sentences.
Is there any way to just create one dictionary for each row instead of a list of dictionaries?
Note: I did look at this question, however it didn't really help.
Here you go. I put the comment into an array
# Import libraries
import csv
import json
import pprint
# Open file
def readPerfReviewCSVToDict(csvPath):
reader = csv.DictReader(open(csvPath, 'rU'))
perfReviewsDictionary = []
for line in reader:
perfReviewsDictionary.append(line)
perfReviewsDictionaryWithCommentsSplit = []
for item in perfReviewsDictionary:
itemId = item["id"]
itemType = item["type"]
itemComment = item["comments"]
itemCommentDictionary = []
itemCommentDictionary = itemComment.split()
perfReviewsDictionaryWithCommentsSplit.append({'id':itemId, 'type':itemType, 'comments':itemCommentDictionary})
return perfReviewsDictionaryWithCommentsSplit
dict_list = readPerfReviewCSVToDict("test.csv")
pprint.pprint(dict_list)
The output is:
[{'comments': ['test', 'ape', 'dog'], 'id': '1', 'type': 'Test'},
{'comments': ['dog'], 'id': '2', 'type': 'Test'}]
Since you haven't given a reproducible example, with a sample DataFrame, I've created one for you
import pandas as pd
df = pd.DataFrame([[1, "Contractor", "Please post"], [2, "Developer", "a reproducible example"]])
df.columns = ['ID', 'ReviewType', 'Comment']
In your computer, instead of doing this, type:
df = pd.read_csv(file_path)
to read in the csv file as a pandas DataFrame.
Now I will create a list, called dictList which will be empty initially, I am going to populate it with a dictionary for each row in the DataFrame df
dictList = []
#Iterate over each row in df
for i in df.index:
#Creating an empty dictionary for each row
rowDict = {}
#Populating it
rowDict['ID'] = df.at[i, 'ID']
rowDict['ReviewType'] = df.at[i, 'ReviewType']
rowDict['Comment'] = df.at[i, 'Comment']
#Once I'm done populating it, I will append it to the list
dictList.append(rowDict)
#Go to the next row and repeat.
Now iterating over the list of dictionaries we have created for my example
for i in dictList:
print(i)
We get
{'ID': 1, 'ReviewType': 'Contractor', 'Comment': 'Please post'}
{'ID': 2, 'ReviewType': 'Developer', 'Comment': 'a reproducible example'}
Do you want this?
DICT = {}
for line in reader:
DICT[line['ID']] = line

Resources