'How can I create a dictionary from a data frame with multiple values? [duplicate]
I am trying to create a dict from a data frame of a stock. The dict keys should be the sector and the values the corresponding tickers.
df.set_index('Sector').to_dict()['Ticker']
I have this code which works in creating some of the dictionary. The issue being it has created the right amount of keys but only one ticker to each key. I can see from the data frame that most keys should include many more tickers.
The DataFrame looks like this:
| Company | Ticker | Exchange | Sector | Date added | Weighting |
|---|---|---|---|---|---|
| 3M | MMM | NYSE | indus | 09/08/76 | 3.022 |
the desired output will be
{Indus: MMM, BA, ....,
Financials: AXP, GS,...}
for all the industry's and tickers
Solution 1:[1]
Not completely sure what you want, but I assume you want to group the sectors and create a list of Tickers for each sector. I created an example dataframe as yours is not containing enough rows. Following would be the code to create the dictionary:
import pandas as pd
df = pd.DataFrame({'Sector':['s1', 's1','s1','s2','s2'],
'Ticker': ['t1','t2','t3','t4','t5'],
'Other column':[1,2,3,4,5]
})
print(df)
result = df.groupby('Sector')['Ticker'].apply(list).to_dict()
print(result)
Dataframe looks like this:
Sector Ticker Other column
0 s1 t1 1
1 s1 t2 2
2 s1 t3 3
3 s2 t4 4
4 s2 t5 5
Dictionary looks like this:
{'s1': ['t1', 't2', 't3'], 's2': ['t4', 't5']}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JANO |
