Tags down


Creating dictionary from excel data

By : Jay
Date : August 02 2020, 05:00 AM
Does that help I have data in excel and need to create a dictionary for those data. , Finally, I solved.
code :
import pandas as pd
from pprint import pprint 
existing_excel_file = 'BHARTI_Model-4_Migration Service parameters - Input sheet_v1.0_DRAFT_26-02-2020.xls'

df_service = pd.read_excel(existing_excel_file, sheet_name='Sheet2')

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)
[{'marks': [90, 95, 98],
  'name': 'dhdn',
  'sub': ['c', 'java', 'python'],
  'usn': '1bm15mca13'},
 {'marks': [80, 81],
  'name': 'paddu',
  'sub': ['c#', 'java'],
  'usn': '1bm15mca17'},
 {'marks': [92, 91],
  'name': 'subbu',
  'sub': ['java', 'perl'],
  'usn': '1bm15mca14'}]

Share : facebook icon twitter icon

Python Creating Dictionary from excel data

Date : March 29 2020, 07:55 AM
Hope this helps I want to create a dictionary from the values, i get from excel cells, My code is below,
code :
d = {}
wb = xlrd.open_workbook('foo.xls')
sh = wb.sheet_by_index(2)   
for i in range(138):
    cell_value_class = sh.cell(i,2).value
    cell_value_id = sh.cell(i,0).value
    d[cell_value_class] = cell_value_id

creating dictionary within a dictionary in python reading some data is a csv file

By : mark
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have a csv file called sample.csv which contains the following data:- , A solution with csv module
code :
import dateutil.parser
import csv

data_dict = {}
with open('data.csv', 'r') as csvfile:
    csvreader = csv.reader(csvfile)
    for row in csvreader:
        dt = dateutil.parser.parse(row[0])
        if not data_dict.get(dt.hour,''):
            data_dict[dt.hour] = {}
        if not data_dict[dt.hour].get(dt.minute,''):

Excel VBA Dictionary: add matching criteria if the data doesn't match with dictionary

By : zhong.dongyu
Date : March 29 2020, 07:55 AM
To fix this issue I have been working on finding a way to add a matching criteria to another workbook almost this day, but I did not find anyway to do it yet. The example scenario is the following, I have two workbooks (workbookA and workbookB) and each workbook has their own "Country" and "Value" lists. Kindly see sample tables per below. , With minimal changes to your code:
code :
Sub Test_match_fill_data()
    Dim Dict As Object
    Dim key As Variant
    Dim aCell As Range, bCell As Range
    Dim i As Long, j As Long
    Dim w1 As Worksheet, w2 As Worksheet

    Set Dict = CreateObject("Scripting.Dictionary")
    Set w1 = Workbooks("workA").Sheets("Sheet1")
    Set w2 = Workbooks("workB").Sheets("Sheet1")

    i = w1.Cells(w1.Rows.Count, 1).End(xlUp).row

    For Each aCell In w1.Range("A6:A" & i)
        Dict(aCell.Value) = aCell.Offset(0, 2).Value

    j = w2.Cells(w2.Rows.Count, 1).End(xlUp).row

    For Each bCell In w2.Range("A6:A" & j)
        If Dict.Exists(bCell.Value) Then
            bCell.Offset(0, 2).Value = Dict(bCell.Value)
            Dict.Remove bCell.Value
        End If

    For Each key In Dict
        With w2.Cells(w2.Rows.Count, 1).End(xlUp).Offset(1)
             .Value = key
            .Offset(,2) = Dict(key)
         End With
End Sub
Sub Test_match_fill_data()
    Dim Dict As Object
    Dim key As Variant
    Dim cell As Range

    Set Dict = CreateObject("Scripting.Dictionary")
    With Workbooks("workA").Sheets("Sheet1")
        For Each cell In .Range("A6", .Cells(.Rows.count, 1).End(xlUp))
            Dict(cell.Value) = cell.Offset(0, 2).Value
    End With

    With Workbooks("workB").Sheets("Sheet1")
        For Each cell In .Range("A6", .Cells(Rows.count, 1).End(xlUp))
            If Dict.Exists(cell.Value) Then
                cell.Offset(0, 2).Value = Dict(cell.Value)
                Dict.Remove cell.Value
            End If
        For Each key In Dict
            With .Cells(.Rows.count, 1).End(xlUp).Offset(1)
                 .Value = key
                .Offset(, 2) = Dict(key)
             End With
    End With
End Sub
Sub Test_match_fill_data()
    Dim Dict As Object
    Dim iItem As Long
    Dim workACountries As Variant, workAValues As Variant
    Dim workBCountries As Variant, workBValues As Variant

    With Workbooks("workA").Sheets("Sheet1")
        workACountries = .Range("A6", .Cells(.Rows.count, 1).End(xlUp)).Value
        workAValues = .Range("C6:C" & .Cells(.Rows.count, 1).End(xlUp).Row).Value
    End With

    Set Dict = CreateObject("Scripting.Dictionary")
    For iItem = 1 To UBound(workACountries)
        Dict(workACountries(iItem, 1)) = workAValues(iItem, 1)

    With Workbooks("workB").Sheets("Sheet1")
        workBCountries = .Range("A6", .Cells(.Rows.count, 1).End(xlUp)).Value
        workBValues = .Range("C6:C" & .Cells(.Rows.count, 1).End(xlUp).Row).Value
    End With

    For iItem = 1 To UBound(workBCountries)
        If Dict.Exists(workBCountries(iItem, 1)) Then
            workBValues(iItem, 1) = Dict(workBCountries(iItem, 1))
            Dict.Remove workBCountries(iItem, 1)
        End If

    With Workbooks("workB").Sheets("Sheet1")
        .Range("A6").Resize(UBound(workBCountries)).Value = workBCountries
        .Range("C6").Resize(UBound(workBCountries)).Value = workBValues

        .Cells(.Rows.count, 1).End(xlUp).Offset(1).Resize(Dict.count).Value = Application.Transpose(Dict.Keys)
        .Cells(.Rows.count, 3).End(xlUp).Offset(1).Resize(Dict.count).Value = Application.Transpose(Dict.Items)
    End With
End Sub

Creating Python Dictionary from excel

By : Hoam Lee
Date : March 29 2020, 07:55 AM
I hope this helps . I want to create a dictionary from the values that imported from excel file, My code is below: , if i have understood your logic, here my solution:
code :
#method to create samples list
nb_s = 3;nb_d = 2

S = ['s' + str(x) for x in range(1, nb_s + 1)]
D = ['d' + str(x) for x in range(1, nb_d + 1)]
C = ['c' + str(x) for x in range(1, (len(S) * len(D)) + 1)]


dico_s = {}
for s in S:
    dico_d = {}
    for d in D:
        idx = D.index(d) + len(D) * S.index(s)
        dico_d[d] = C[idx]
    dico_s[s] = dico_d

S ->['s1', 's2', 's3']
D ->['d1', 'd2']
C->['c1', 'c2', 'c3', 'c4', 'c5', 'c6']

DICO->{'s1': {'d1': 'c1', 'd2': 'c2'}, 
       's2': {'d1': 'c3', 'd2': 'c4'}, 
       's3': {'d1': 'c5', 'd2': 'c6'}}

**for nb_s = 4 and nb_d = 6**

S -> ['s1', 's2', 's3', 's4']
D -> ['d1', 'd2', 'd3', 'd4', 'd5', 'd6']
C -> ['c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10', 'c11', 'c12', 
      'c13', 'c14', 'c15', 'c16', 'c17', 'c18', 'c19', 'c20', 'c21', 'c22', 'c23', 'c24']

DICO -> {'s1': {'d1': 'c1', 'd2': 'c2', 'd3': 'c3', 'd4': 'c4', 'd5': 'c5', 'd6': 'c6'}, 
         's2': {'d1': 'c7', 'd2': 'c8', 'd3': 'c9', 'd4': 'c10', 'd5': 'c11', 'd6': 'c12'}, 
         's3': {'d1': 'c13', 'd2': 'c14', 'd3': 'c15', 'd4': 'c16', 'd5': 'c17', 'd6': 'c18'}, 
         's4': {'d1': 'c19', 'd2': 'c20', 'd3': 'c21', 'd4': 'c22', 'd5': 'c23', 'd6': 'c24'}}

python creating dictionary from excel

By : Yogya raj
Date : March 29 2020, 07:55 AM
wish of those help I have an excel sheet with 2 columns. Column 1 is name, and Column 2 is age. I want to create a dictionary where name is key and age is value. Here is the code, but it is creating a dictionary incorrectly. , That's quite simple with pandas:
Related Posts Related Posts :
  • Uninstalling python3 on MacOs
  • Python RSA key, recieved the key but getting error "This is not a private key"
  • How to send a GraphQL query to AppSync from python?
  • Chunking for non-noun phrases in SpaCy
  • Sum columns of a dataframe iteratively?
  • To stop the script when you run another script
  • Runt a bash script every 10 seconds between certain hours
  • How to extract text from under headings in a docx file using python
  • What is the recommended way to connect to HBase using Python?
  • Duplicating rows in a pandas dataframe
  • Machine Learning Classification Model with Sklearn runs OK but doesn't pass test
  • Move files from one bucket to a folder inside another bucket
  • How to Trigger particular extension file in cloud storage through google cloud functions
  • finding out how many values matched in a particular column for a particular row in a dataframe in Python
  • f returning value of a variable inside of g in Python 3.x
  • How to return instead of print
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org