Home > Blockchain >  Need to Return a Single Value from a Dictionary Key with Multiple Values (Python)
Need to Return a Single Value from a Dictionary Key with Multiple Values (Python)

Time:03-20

I have a data frame that I can't parse on position because it's not consistent, so I need to translate it to a dictionary. I need to take this dataframe

index TimeStamp1 TimeStamp2 Timestamp3
Bird 1 2 3
Snack 4 5 6

and get these dictionary values: Bird: 1 Snake: 4, 5, 6

Any advice on how to do this? I tried making a dictionary and orienting it on the dictionary but I can't figure out how to select a single value.

I'm starting with yf.Ticker(ticker).quarterly_balance_sheet - which returns a dataframe I need to be able to index the values of the dataframe.

CodePudding user response:

Create a dictionary where you assign a list as a value of dictionary.

dict={"bird": [1,2,3], "snack": [4,5,6]}
#select a list from dictionary
print(dict["bird"])
#select an element from a value of list dict
print(dict["bird"][0])

Then you can access to a single element, or to all other elements.

CodePudding user response:

Since it returns a DataFrame, you can use to_dict to obtain a dictionary like object. Try this:

msft = yf.Ticker("MSFT")
balance_sheet = msft.quarterly_balance_sheet
split_dict = balance_sheet.to_dict('split')
result = {
    key: split_dict['data'][idx] for idx, key in enumerate(split_dict['index'])
}
print(result)
# >> {"bird": [1,2,3], "snack": [4,5,6]}
  • Related