Skip to content
Advertisement

How would I pull out similar value pairs from a dictionary and put them in a new dictionary?

I have a dictionary filled with 3 keys: "Ticker", "Title" and "Value". The ticker key contains 100 stock tickers that corresponds to the value of the purchase and title that are in the same position. So here is an example of the dictionary:

{'Ticker': {0: 'AKUS', 1: 'HHC', 2: 'IOVA', 3: 'DDOG'},
 'Title': {0: '10%', 1: 'Dir, 10%', 2: 'Dir', 3: 'Dir'},
 'Value': {0: '+$374,908,350', 1: '+$109,214,243', 2: '+$65,000,000', 3: '+$49,999,940'}}

So "AKUS" corresponds with the 10% and +$374,908,350.

I am only showing 4 items in the dictionary but my actual dictionary has 100.

My question is regarding a new dictionary that only contains tickers and values but everything in that dictionary has the same title.

For example, I want to create a 10% dictionary that contains all the tickers and values of stocks where the title contained 10%.

I know some stocks have multiple titles but I don’t mind the same stocks being in more than one dictionary. Would some one be able to let me know how I should go about doing this? Thank you in advance, I have been stuck on this for a while.

Advertisement

Answer

Simple to do using pandas if you are OK using that; so assuming your dictionary is named d:

df = pd.DataFrame.from_dict(d)
df10 = df[df['Title'].str.contains('10%')]
print(df10)

produces

  Ticker     Title          Value
0   AKUS       10%  +$374,908,350
1    HHC  Dir, 10%  +$109,214,243
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement