batrick_pateman
batrick_pateman

Reputation: 29

Defining each list in a FOR loop in Python

I have filtered some data I am using for some analysis and have sorted the tickers into the lists I desire, but I cannot access the lists outside the for loop?

import pandas as pd

data = pd.read_csv('https://datahub.io/core/s-and-p-500-companies/r/constituents.csv')
# Classify sectors
sectors = list(data.Sector)
sectors = set(sectors)

for i in sectors:
    i = data.loc[data['Sector'] == i]
    i = list(i.Symbol)
    print(i)

I cannot understand why the lists are not able to called from outside the for loop, thought the lists are being grouped correctly (as seen by printing i).

Thanks so much for any help.

Upvotes: 0

Views: 64

Answers (1)

Jab
Jab

Reputation: 27495

This can be done using pandas entirely. Below is one way it can be done:

import pandas as pd
from operator import attrgetter

df = pd.read_csv('https://datahub.io/core/s-and-p-500-companies/r/constituents.csv')

df1 = pd.DataFrame(df.groupby(df.Sector)['Symbol'].apply(attrgetter('values')).values.tolist(), index=df.Sector.unique())
print(df1)

0      1     2     3   ...    70    71    72    73
Industrials             ATVI  GOOGL  GOOG     T  ...  None  None  None  None
Health Care              AAP   AMZN  APTV   AZO  ...  None  None  None  None
Information Technology   ADM     MO  BF.B   CPB  ...  None  None  None  None
Communication Services   APA    BKR   COG   CVX  ...  None  None  None  None
Consumer Staples         AFL    ALL   AXP   AIG  ...  None  None  None  None
Consumer Discretionary   ABT   ABBV  ABMD     A  ...  None  None  None  None
Utilities                MMM    AOS   ALK  ALLE  ...   GWW   WAB    WM   XYL
Financials               ACN   ADBE   AMD  AKAM  ...    WU  XLNX  ZBRA  None
Materials                APD    ALB  AMCR   AVY  ...  None  None  None  None
Real Estate              ARE    AMT   AVB   BXP  ...  None  None  None  None
Energy                   AES    LNT   AEE   AEP  ...  None  None  None  None

[11 rows x 74 columns]

Upvotes: 2

Related Questions