Skip to content

Tag: python

Creating dictionary need more than 1 value to unpack

I have two lists: I also have a json file for each manager: So now, I want to create a dictionary by to categorize data by site! which is making it challenging for me. The output I need is to show employees for each site. Here is the code I have: The error I get: Answer Instead of looping over

Convert the lists in multiple columns to rows in pandas df

I have df with three columns: Col 0 – sentence row num Col 1 – Sentence converted to list Col 2 – list of annotations Col 0 Col1 Col2 1 [This, is, sentence] [l1, l2, l3] 2 [This, is, sentence, too] [l1, l2, l3, l4] I would like to convert Col1 and Col2 and move each row and its respective

Generic ParamSpec on Python 3.9

The following is correct in Python 3.10, but not 3.9: Running the above triggers a TypeError: What must I do to get this piece of code to run in Python 3.9? I get it that ParamSpec is a 3.10 feature. But typing-extensions is supposed to make the code backward-compatible, right? I’m guessing that I must …

python multiply list elements inside list

I use lists to randomly pick an element over many iterations (to create artificial data sets). To change the probability of getting a certain element, I’m repeatedly adding those elements that should have a higher change of being picked, so instead of I would do Is there a better way to do this inline? …

Convert nested dictionary to pandas dataframe

I have a nested dictionary as below: I need to convert it into a dataframe like below I have tired the following code from this answer I am getting the dataframe like below This is the closest I got to the desired output. What changes do I need to make to the code to get the desired dataframe? Answer Change