[Section2] Pandas Series Fundamentals

Jinyoung Cheon·2025년 1월 11일

해당 내용은 Udemy 강의 'Pandas 및 Python을 이용한 데이터 분석:마스터 클래스'를 수강 후 정리한 내용입니다.

https://www.udemy.com/course/best-pandas-python

Section1

DEFINE A PANDAS SERIES (WITH NUMERIC DEFAULT INDEX)

MINI CHALLENGE #1:

  • Define a Pandas Series named "my_series" that contains your top 3 favourite movies. Confirm the datatype of "my_series"
my_list = ['서울의 봄', '친구', '신과함께']
my_series = pd.Series(data = my_list)
my_series
type(my_series)

Section2

DEFINE A PANDAS SERIES WITH CUSTOM INDEX

MINI CHALLENGE #2:

  • Define a Pandas Series named "my_series" that contains your top 3 favourite movies. Instead of using default numeric indexes (similar to mini challenge #1), use the following indexes "movie #1", "Movie #2", and "movie #3"
my_list = ['서울의 봄', '친구', '신과함께']
my_labels = ['movie#1', 'movie#2', 'movie#3']
print(my_labels)
my_series = pd.Series(data = my_list, index = my_labels)
print(my_series)

Section3

DEFINE A PANDAS SERIES FROM A DICTIONARY

MINI CHALLENGE #3:

  • Create a Pandas Series from a dictionary with 3 of your favourite stocks and their corresponding prices
stock_dict = {'GOOG': 193.17,
            'META': 615.86,
            'AMZN': 218.94}

my_stock = pd.Series(data = stock_dict)

print(my_stock)

Section4

PANDAS ATTRIBUTES

  • Attributes/Properties: do not use parantheses "()" and are used to get Pandas Series Properties.
  • Methods: use parantheses "()" and might include arguments and they actually alter/change the Pandas Series.
  • Indexers: use square brackets "[]" and are used to access specific elements in a Pandas Series or DataFrame.

MINI CHALLENGE #4:

  • What is the size of the Pandas Series? (External Research for the proper attribute is Required)
my_series.size

Section5

PANDAS METHODS

MINI CHALLENGE #5:

  • Show the last 2 rows in the Pandas Series (External Research is Required)
  • How many bytes does this Pandas Series consume in memory? (External Research is Required)
my_series.tail(2)
my_series.memory_usage()

Section6

IMPORT CSV DATA (1-D) USING PANDAS

MINI CHALLENGE #6:

  • Set Squeeze = False and rerun the cell, what do you notice? Use Type to compare both outputs

squeeze 매개변수는 제거되었기 때문에 MINI CHALLENGE #6는 PASS


Section7

PANDAS BUILT-IN FUNCTIONS

Built-in Functions

MINI CHALLENGE #7:

  • Given the following Pandas Series, convert all positive values to negative using python built-in functions
  • Obtain only unique values (ie: Remove duplicates) using python built-in functions
  • my_series = pd.Series(data = [-10, 100, -30, 50, 100])
my_series = pd.Series(data = [-10, 100, -30, 50, 100])
my_series
abs(my_series)
set(my_series)

Section8

SORTING PANDAS SERIES

MINI CHALLENGE #8:

  • Sort the S&P500 values in a decending order instead. Make sure to update values in-memory.
sp500 = sp500.sort_values(ascending=False)

Section9

PERFORM MATH OPERATIONS ON PANDAS SERIES

MINI CHALLENGE #9:

  • Obtain the average price of the S&P500 using two different methods

Section10

CHECK IF A GIVEN ELEMENT EXISTS IN A PANDAS SERIES

# Check if a given number exists in a Pandas Series values
# Returns a boolean "True" or "False"
1295.500000 in sp500.values
# Check if a given number exists in a Pandas Series index
1295.500000 in sp500.index
# Note that by default 'in' will search in Pandas index and not values

MINI CHALLENGE #10:

  • Check if the stock price 3349 exists in the sp500 Pandas Series or not
  • Round stock prices to the nearest integer and check again
    sp500 = round(sp500)
    
    3349 in sp500.values

Section11

INDEXING: OBTAIN SPECIFIC ELEMENTS FROM PANDAS SERIES

MINI CHALLENGE #11:

  • Obtain the fifth element in the Pandas Series
sp500[4]

Section12

SLICING: OBTAIN MULTIPLE ELEMENTS FROM PANDAS SERIES

MINI CHALLENGE #12:

  • Obtain all elements in Pandas Series except for the last 3 elements
sp500[:-3]
profile
데이터를 향해, 한 걸음씩 천천히.

0개의 댓글