Pandas Zoo
๐ผ A zoo for pandas methods, functions, usages! ๐ผ
Search for any feature you need by key words.
A short code snippet will be attached to the features so you can easily apply it to your work.
before copy & paste the snippet, make sure you imported packages as below.
import pandas as pd
import numpy as np
ํ๋ค์ค์ ๋ค์ํ ๊ธฐ๋ฅ์ ์๊ฐํ๋ repo ์ ๋๋ค!
์ํ๋ ๊ธฐ๋ฅ์ ๋ฐ๋ก ์ฐพ์ ์ธ ์ ์๋๋ก ์ ๋ฆฌํ ์์ ์ ๋๋ค.
์ฌ์ฉํ๊ณ ์ ํ๋ ๋ชฉ์ ์ ๋ง๊ฒ ๋ฐ๋ก ์ ์ฉํ ์ ์๋๋ก, ์ฝ๋ ์ค๋ํซ์ ์ฒจ๋ถํฉ๋๋ค.
์ฝ๋ ๋ณต๋ถ ์ ์, ํ๋ค์ค์ ๋ํ์ด๋ฅผ ์ ํธ์ถ ํ์ จ๋์ง ํ์ธํด ์ฃผ์ธ์.
Example
- ์ฌ์ฉ์ ์ง์ ํจ์ ์ ์ฉํ๊ธฐ (Applying lambda function to Pandas Series)
[ํค์๋ key words]
- ๋๋ค, ํจ์, ๋งตํ
- apply, mapping, custom function, column
# daily bamboo and water consumption in pandas zoo
panda_consumption = {'Day' : [1, 2, 3, 4, 5, 6],
'Cum_Bamboo': [20, 30, 60, 100, 120, 150],
'Cum_Water': [15, 30, 45, 70, 90, 120],
}
# make dataframe
DF = pd.DataFrame.from_dict(panda_consumption)
# add 50 to every consumption of Water
DF.Cum_Water.apply(lambda x: x+50)
Contents
- ๋ฐ์ดํฐ ํ๋ ์ DataFrame
- ์กฐํํ๊ธฐ Descriptive
- ๊ฒฐ์ธก์น ์ฒ๋ฆฌ Missing Values and Imputation
- ํ/์ด์ ์กฐ๊ฑด ๊ฑธ๊ธฐ Filter
- ์ธ๋ฑ์ฑ Indexing
- ํจ์ ์ ์ฉํ๊ธฐ Apply a Function
- ๊ทธ๋ฃน๋ณ ์กฐ์ํ๊ธฐ Groupby / Aggregating
- ์๊ณ์ด Time Series
- ์๊ฐํ Plot
- ๊ธฐํ Etc
click here!
1. ๋ฐ์ดํฐ ํ๋ ์ ๋ง๋ค๊ธฐ Create and Load-
๋ฆฌ์คํธ๋ก ์๋ก์ด ๋ฐ์ดํฐํ๋ ์ ๋ง๋ค๊ธฐ (Creating new DataFrame from lists)
-
๋์ ๋๋ฆฌ๋ก ์๋ก์ด ๋ฐ์ดํฐ ํ๋ ์ ๋ง๋ค๊ธฐ (Creating new DataFrame from dict of lists)
-
๋ณต์์ ๋ฐ์ดํฐ ํ๋ ์ ๋ณํฉํ๊ธฐ (merging multiple(more than 2) DataFrames)
-
๊ธฐ์กด์ ๋ฐ์ดํฐํ๋ ์์ ์๋ก์ด ํ ์ถ๊ฐํ๊ธฐ (Appending a new row to an existing DataFrame)
-
์๋ก์ด ์ด์ ์ถ๊ฐํ์ง ์๊ณ ๊ธฐ์กด์ ์ด์ ์๋ก์ด ๊ฐ์ ๋ณํฉํ๊ธฐ (replacing columns using existing DF)
pd.update
click here!
2. ์กฐํํ๊ธฐ Descriptive- ๊ธฐ์ ํต๊ณ๋ ์กฐํํ๊ธฐ (Getting desriptive statistics from dataframe)
- ์ด ๋ณ๋ก ๋ค์ํ ๊ธฐ์ ํต๊ณ๋ ํ์ธํ๊ธฐ (Checking specific statistics from columns)
- ์ด ๋ด์ ์ ๋ํฌํ ์นดํ ๊ณ ๋ฆฌ ๋ณ๋ก ๊ฐ์/๋น์จ ๊ตฌํ๊ธฐ (Counting numbers of sample / Calculting proportion per category in a column)
click here!
3. ๊ฒฐ์ธก์น ์ฒ๋ฆฌ Missing Values and Imputation-
์ด ๋ณ๋ก ๊ฒฐ์ธก์น๊ฐ ํฌํจ๋ ๋ ์ฝ๋์ ์ ์ธ๊ธฐ (Determining the number of NA values in columns)
-
๊ฒฐ์ธก ํฌํจ๋ ํ/์ด ๋ ๋ฆฌ๊ธฐ (Droping rows/cols with NA values) pd.dropna
click here!
4.ํ/์ด์ ์กฐ๊ฑด ๊ฑธ๊ธฐ Filter-
ํน์ ๋จ์ด๋ฅผ ํฌํจํ row๋ง ๊ณจ๋ผ๋ด๊ธฐ (Getting rows that contain specific text/word/string)
pd.Series.str.contains -
๋ฐ์ดํฐํ๋ ์ ์ด(์๋ฆฌ์ฆ) ๋ด์์ ๋ฆฌ์คํธ ๋ด์ ํน์ ๊ฐ์ด ์๋ ํ ๊ณจ๋ผ๋ด๊ธฐ (Using a list of values to select rows from Data Frame) pd.Series.isin
-
์ค๋ณต๋๋ ํ ์ ๊ฑฐํ๊ธฐ, ์ด(๋ณต์ ๊ฐ๋ฅ) ๊ธฐ์ค ํน์ ์ ์ฒด ํ ๊ธฐ์ค (Droping duplicated/repeated/redundant rows, in respect to column(s) or full row) pd.drop_duplicates
click here!
5. ์ธ๋ฑ์ฑ Indexing-
loc๋ก ์๋ก์ด ์นผ๋ผ ํ ๋นํ๊ธฐ (Assigning a new column using loc method)
-
์ํ๋ ๋ฐ์ดํฐ ํ์ ์ ์นผ๋ผ๋ง ์ ํํ๊ธฐ (Selecting columns by specific data types)
click here!
6. ํจ์ ์ ์ฉํ๊ธฐ Apply- ์ฌ์ฉ์ ์ง์ ํจ์ ์ ์ฉํ๊ธฐ (Applying lambda function to Pandas Series)
- ๋ฌธ์์ด ํ์์ ๋ง์ง๋ง n๊ฐ ๋จ์ด ์ทจํ๊ธฐ (getting last n characters from a string column)
- ์ด์ if else ์กฐ๊ฑด๋ฌธ ์ ์ฉํ๊ธฐ (applying if else statement to a column)
- apply์ ์ฌ๋ฌ๊ฐ ์ธ์ ๋ฃ์ด์ฃผ๊ธฐ (multiple argument for apply statement in DF)
click here!
7. ๊ทธ๋ฃน๋ณ ์กฐ์ํ๊ธฐ Groupby-
๊ทธ๋ฃน ๋ณ๋ก ํ๊ท ๊ฐ ๊ตฌํ๊ธฐ (Getting average value of each column per group)
-
๊ทธ๋ฃน ๋ณ๋ก Multi level one-hot encoding ํ๊ธฐ (one-hot encoding for multi-level column data)
click here!
8. ์๊ณ์ด Time Series- ํ ๋ณ ์ฐจ์ด ๊ตฌํ๊ธฐ, ์ฐจ๋ถ (Getting difference between rows)
click here!
9. ์๊ฐํ Plot- ํ๋ค์ค ์๋ฆฌ์ฆ ๋ฐ์ดํฐ ์๊ฐํํ๊ธฐ (Drawing a quick plot using pandas series)
click here!
10. ๊ธฐํ etc- ์ถ๋ ฅ ๊ฒฐ๊ณผ ํ์ฅํ๊ธฐ (Expanding pandas output to be shown)