Statement of Completion#5ac8b0a9
Intro to Pandas for Data Analysis
easy
Series Practice with S&P Companies' Market Cap
Resolution
Activities
Take a look at the raw data:¶
Company stock symbols
In [116]:
!head sp500-symbols.csv
Name,Symbol 3M Company,MMM A.O. Smith Corp,AOS Abbott Laboratories,ABT AbbVie Inc.,ABBV Accenture plc,ACN Activision Blizzard,ATVI Acuity Brands Inc,AYI Adobe Systems Inc,ADBE Advance Auto Parts,AAP
Market cap raw data:
In [117]:
!head sp500-marketcap.csv
Symbol,Market Cap MMM,138721055226 AOS,10783419933 ABT,102121042306 ABBV,181386347059 ACN,98765855553 ATVI,52518668144 AYI,6242377704 ADBE,94550214268 AAP,8123611867
In [118]:
import pandas as pd
In [119]:
market_cap = pd.read_csv("sp500-marketcap.csv", index_col="Symbol")['Market Cap']
market_cap.head()
Out[119]:
Symbol MMM 138721055226 AOS 10783419933 ABT 102121042306 ABBV 181386347059 ACN 98765855553 Name: Market Cap, dtype: int64
In [120]:
symbols = pd.read_csv("sp500-symbols.csv", index_col="Name")['Symbol']
symbols.head()
Out[120]:
Name 3M Company MMM A.O. Smith Corp AOS Abbott Laboratories ABT AbbVie Inc. ABBV Accenture plc ACN Name: Symbol, dtype: object
Basic Series Attributes¶
1. Name of the market_cap
Series¶
In [ ]:
2. Name of the symbols
Series¶
In [ ]:
3. What's the dtype of market_cap
¶
In [ ]:
4. What's the dtype of symbols
¶
In [ ]:
5. How many elements do the series have?¶
In [ ]:
6. What's the minimum value for Market Cap?¶
In [ ]:
7. What's the maximum value for Market Cap?¶
In [ ]:
8. What's the average Market Cap?¶
In [ ]:
9. What's the median Market Cap?¶
In [ ]:
Selection and Indexing¶
In [121]:
market_cap.head()
Out[121]:
Symbol MMM 138721055226 AOS 10783419933 ABT 102121042306 ABBV 181386347059 ACN 98765855553 Name: Market Cap, dtype: int64
10. What's the symbol of Oracle Corp.
?¶
In [122]:
symbols.loc["Oracle Corp."]
Out[122]:
'ORCL'
11. What's the Market Cap of Oracle Corp.
?¶
In [123]:
market_cap.loc["ORCL"]
Out[123]:
202302349740
12. What's the Market Cap of Wal-Mart Stores
?¶
In [124]:
symbols.loc["Wal-Mart Stores"]
market_cap.loc["WMT"]
Out[124]:
304680931618
13. What's the symbol of the 129th company?¶
In [125]:
symbols.iloc[128]
Out[125]:
'STZ'
14. What's the Market Cap of the 88th company in symbols
?¶
In [126]:
symbols.iloc[87]
market_cap.loc["CPB"]
Out[126]:
13467193376
15. Create a new series only with FAANG Stocks¶
In [127]:
faang_market_cap = market_cap[symbols[["Amazon.com Inc", "Apple Inc.", "Microsoft Corp.", "Alphabet Inc Class A", "Facebook, Inc.", "Netflix Inc.", ]]]
16. Select the market cap of companies in position 1st, 100th, 200th, etc.¶
In [129]:
position_companies = market_cap.iloc[[0, 99, 199, 299, 399, 499]]
Sorting Series¶
17. What's the 4th company sorted lexicographically by their symbol?¶
In [134]:
symbols_sorted = symbols.sort_values()
symbols_sorted.iloc[3]
Out[134]:
'AAPL'
18. What's the Market Cap of the 7th company (in descending order)?¶
In [141]:
market_cap_sorted = market_cap.sort_index(ascending = False)
market_cap_sorted.iloc[6]
Out[141]:
13390513478
In [ ]: