Search code examples
pythonlistpandasyahoo-finance

Grouping similar values in a df for yahoo finance


I have a a df that looks like this (although it extends to the whole SP500):

        Sector
Symbol
MMM     XLI
ABT     XLV
ABBV    XLV
ACN     XLK
ATVI    XLK

My question is, how can I group the symbols together based on sectors? Eg, when I want to access the data, i want to have all symbols grouped by sector.

So far I have tried:

sector_list = list(df[df['Sector']=='XLV'].index)

This works, but only works for ONE sector at a time. I want to calculate returns of all 10 sectors at the same time, so I need an equation that can return ALL of them at once, grouped by sector


Solution

  • Use groupby with apply and convert all index values to list:

    s = df.groupby('Sector').apply(lambda x: x.index.tolist())
    print (s)
    Sector
    XLI          [MMM]
    XLK    [ACN, ATVI]
    XLV    [ABT, ABBV]
    dtype: object
    

    Or reset_index for column Symbol from index values and then groupby by Sector and create list from column Symbol per group by GroupBy.apply:

    s = df.reset_index().groupby('Sector')['Symbol'].apply(list)
    print(s)
    Sector
    XLI          [MMM]
    XLK    [ACN, ATVI]
    XLV    [ABT, ABBV]
    Name: Symbol, dtype: object