본문 바로가기
IT World/Python

Python Training Day 10 & 11. Pandas Basics, Generators

by 닷라인웨이 2022. 12. 23.

Chapter 13. Pandas Basics

 

Pandas DataFrames

It is built on the Numpy package and its key data structure is called the DataFrame.

There are several ways to create a DataFrame. One way is to use a dictionary.

Another way to create a DataFrame is by importing a csv file using Pandas.

dict = {"country": ["Brazil", "Russia", "India", "China", "South Africa"],
       "capital": ["Brasilia", "Moscow", "New Dehli", "Beijing", "Pretoria"],
       "area": [8.516, 17.10, 3.286, 9.597, 1.221],
       "population": [200.4, 143.5, 1252, 1357, 52.98] }

import pandas as pd
brics = pd.DataFrame(dict)

# Set the index for brics
brics.index = ["BR", "RU", "IN", "CH", "SA"]

# Import the cars.csv data: cars
cars = pd.read_csv('cars.csv')

 

Indexing DataFrames

There are several ways to index a Pandas DataFrame. One of the easiest ways to do this is by using square bracket notation.

Square brackets can also be used to access observations (rows) from a DataFrame.

You can also use loc and iloc to perform just about any data selection operation. loc is label-based, which means that you have to specify rows and columns based on their row and column labels. iloc is integer index based, so you have to specify rows and columns by their integer index like you did in the previous exercise.

# Import pandas and cars.csv
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)

# Print out country column as Pandas Series
print(cars['cars_per_cap'])

# Print out country column as Pandas DataFrame
print(cars[['cars_per_cap']])

# Print out DataFrame with country and drives_right columns
print(cars[['cars_per_cap', 'country']])

# Print out first 4 observations
print(cars[0:4])

# Print out fifth and sixth observation
print(cars[4:6])

# Print out observation for Japan
print(cars.iloc[2])

# Print out observations for Australia and Egypt
print(cars.loc[['AUS', 'EG']])

 

 

Chapter 14. Generators

 

Generators are used to create iterators, but with a different approach. Generators are simple functions which return an iterable set of items, one at a time, in a special way.

When an iteration over a set of item starts using the for statement, the generator is run. Once the generator's function code reaches a "yield" statement, the generator yields its execution back to the for loop, returning a new value from the set. 

 

Exercise

Write a generator function which returns the Fibonacci series. They are calculated using the following formula: The first two numbers of the series is always equal to 1, and each consecutive number returned is the sum of the last two numbers.

 

 

The more references about Generators

 

Python에서 yield란

yield와 return의 차이점을 이해하고 generator와 어떤 연관이 있는지 알아보자

https://qqplot.github.io/python/2022/02/12/python_yield.html

 

Python에서 yield란

yield와 return의 차이점을 이해하고 generator와 어떤 연관이 있는지 알아보자 사이킷런 api를 살펴보다가 yield라는 예약어(reserved word)가 나와서 뭔가 했다. 어디선가 봤던 거 같은데 그 역할이 가늠이

qqplot.github.io

 

파이썬의 yield 키워드와 제너레이터(generator)

https://www.daleseo.com/python-yield/

 

파이썬의 yield 키워드와 제너레이터(generator)

Engineering Blog by Dale Seo

www.daleseo.com

댓글