Skip to content
Advertisement

Tag: dictionary

Regex removes certain words from my string – Python

The below code is to lookup a dictionary and replace string with values corresponding to dict’s key. Can someone help me understand why my code omits certain words? It removes lh preceeded and followed with a . i.e., lh. and .lh. How to overcome this? I get the output left hand l.h. -left hand- l.h plh phli lhp 1lh lh1

Converting dataframe to dictionary with country by continent

I have a .csv and dataframe which has 2 columns (country, continent). I want to create a dictionary, carrying the continent as key and a list of all countries as values. The .csv has the following format: country continent Algeria Africa Angola Africa and so on. I tried using: But this gave me the following output: Which is the right

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

Sorting list based on dictionary keys in python

Is there a short way to sort a list based on the order of another dictionary keys? suppose I have: I want to sort the list to be [‘a’,’b’,’c’] based on the order of dic keys. Answer You can create a lookup of keys versus their insertion order in dic. To do so you can write: Using this you can

Advertisement