[신] 라이브러리 「pandas」 기초

Joel Lee·2023년 5월 10일
0

AI & DS

목록 보기
7/14
post-thumbnail

본 노트는 Aidemy의 강의, 라이브러리 pandas기초의 노트입니다.

[!abstract]+ Curriculum
1. pandas 란
2. pandas 조작방법
3. 복수의 DataFrame 이용
첨삭문제


Pandas 란

테이블 데이터를 다루는 데 특화된 라이브러리.
Pandas vs. NumPy|600


Pandas 조작

데이터 읽고 쓰기

#pd/read #pd/save

  • pd.read_csv() : sep, header, names, encodinng
  • pd.read_excel() : 'Sheet1'
  • .to_csv(), .to_excel() : index, header

데이터 조작 : 생성, 타입, 참조, 정렬, 추가, 삭제

데이터 생성

#pd/new

  • Series 생성 : pd.Series(dic)
  • DataFrame 생성 : pd.DataFrame(dic) or .DataFrame([Series1, Series2, ... ])

데이터 타입

#pd/type

  • 타입 확인 : .dtypes()
  • 타입 변경 : .astype()
df["price"] = df["price"].astype(float) # 非破壊なメソッドなのでイコールを用いて上書き

데이터 참조

#pd/access

  • Series : 슬라이스는 자동적으로 대괄호 취급을 받는다고 생각하면 됨
series[1]
print(series[0:2])
series[['orange','peach']]
series["orange":]
series['orange']
  • df
    - head, tail
    - loc : 인덱스 슬라이스에서 마지막 인덱스까지 포함함.
    - iloc : 인덱스 슬라이스는 늘 하던 대로.
print(df.head())
print(df.tail(10))
print(df.loc[1:2,"product":])

>>>出力結果
  product  price  quantity
1  orange   1000         2
2  orange    800         1

#pd/access/bull

  • Bull 대수를 사용한 참조
import pandas as pd

food = {"snack": 200, "ra-men": 1000, "rice": 800, "coffee": 100, "green-tea":250, "wine":900}
series = pd.Series(food)
conditions = [True, True, False, False, False, False]

print(series[conditions])

>>>出力結果
snack      200
ra-men    1000
dtype: int64
import pandas as pd

data = {"store": ["shibuya", "shinjuku", "yokohama", "shibuya", "shinjuku", "shibuya"],
        "product": ["banana", "orange", "orange", "grape", "banana", "peach"],
        "price": [200, 1000, 800, 100, 250, 900],
        "quantity": [1, 2, 1, 2, 3, 2]}
df = pd.DataFrame(data)

print(df["price"]>500)
print()
print(df[df["price"]>500])

>>>出力結果
0    False
1     True
2     True
3    False
4    False
5     True
Name: price, dtype: bool

      store product  price  quantity
1  shinjuku  orange   1000         2
2  yokohama  orange    800         1
5   shibuya   peach    900         2

데이터 정렬

#pd/sort

  • Series : 인덱스 정렬, 값 정렬 : ascending
print(series.sort_index())
series.sort_values(ascending=False)
  • df : 열을 지정해서 정렬. 다중 정렬 가능.
print(df.sort_values(by=["quantity", "price"], ascending=True))

데이터 추가, 삭제

#pd/append

  • Series : Series 를 append. 비파괴이므로 대입 필요.
grape = {"grape": 3} # 既に作成したseriesにgrapeを追加
series = series.append(pd.Series(grape)) # 非破壊なメソッドなのでイコールを用いて上書き
  • df
    - 인덱스 방향 : Series 를 append. 혹시 기존에 없던 새로운 열을 추가하는 경우, 기존 인덱스들에 대해선 새 열의 값이 NaN 으로 설정됨.
    - 칼럼 방향 : dic 에 새 요소 추가하듯이 하면 됨.
import pandas as pd

data = {"store": ["shibuya", "shinjuku", "yokohama", "shibuya", "shinjuku"],
        "product": ["banana", "orange", "orange", "grape", "banana"],
        "price": [200, 1000, 800, 100, 250],
        "quantity": [1, 2, 1, 2, 3]}
df = pd.DataFrame(data)

print(df)  # 元のDataFrameの出力
print()  # 改行

series = pd.Series(
    {"store": "shibuya", "product": "peach", "price": 900, "quantity": 2, "month": "August"})
# Seriesの作成(DataFrameにはmonthカラムは存在しない)
df = df.append(series, ignore_index=True)

print(df)  # 追加後のDataFrameの出力

>>>出力結果
     store product  price  quantity
0   shibuya  banana    200         1
1  shinjuku  orange   1000         2
2  yokohama  orange    800         1
3   shibuya   grape    100         2
4  shinjuku  banana    250         3

      store product  price  quantity   month
0   shibuya  banana    200         1     NaN
1  shinjuku  orange   1000         2     NaN
2  yokohama  orange    800         1     NaN
3   shibuya   grape    100         2     NaN
4  shinjuku  banana    250         3     NaN
5   shibuya   peach    900         2  August
import pandas as pd

data = {"store": ["shibuya", "shinjuku", "yokohama", "shibuya", "shinjuku"],
        "product": ["banana", "orange", "orange", "grape", "banana"],
        "price": [200, 1000, 800, 100, 250],
        "quantity": [1, 2, 1, 2, 3]}
df = pd.DataFrame(data)

print(df)
print()  # 改行
df["month"] = ["August", "September", "November",
               "January", "October"]  # カラムmonthを追加

print(df)

>>>出力結果
     store product  price  quantity
0   shibuya  banana    200         1
1  shinjuku  orange   1000         2
2  yokohama  orange    800         1
3   shibuya   grape    100         2
4  shinjuku  banana    250         3

      store product  price  quantity      month
0   shibuya  banana    200         1     August
1  shinjuku  orange   1000         2  September
2  yokohama  orange    800         1   November
3   shibuya   grape    100         2    January
4  shinjuku  banana    250         3    October

#pd/del

  • Series
# バナナのインデックスを削除
series = series.drop("banana") # 非破壊なメソッドなのでイコールを用いて上書き
  • df : axis 지정으로 인덱스 뿐만 아니라 칼럼 삭제도 가능.
# インデックス1,2を削除
df = df.drop(range(0, 2)) # 非破壊なメソッドなのでイコールを用いて上書き
df = df.drop("store", axis=1) # カラムstoreの削除

DataFrame 을 이용한 데이터 분석

DataFrame 의 계산 처리

#pd/cal

  • 칼럼끼리 사칙연산 가능
import pandas as pd
import numpy as np

data = {"apple": [6, 1, 4, 4, 8],
        "orange": [1, 4, 5, 6, 3],
        "banana": [6, 10, 9, 10, 5],
        "strawberry": [3, 4, 9, 2, 4],
        "kiwifruit": [10, 10, 1, 5, 8]
        }
df = pd.DataFrame(data)

print(df)
print() # 改行
print(df['apple']+df["strawberry"])  # カラム同士の足し算

>>>出力結果
   apple  orange  banana  strawberry  kiwifruit
0      6       1       6           3         10
1      1       4      10           4         10
2      4       5       9           9          1
3      4       6      10           2          5
4      8       3       5           4          8

0     9
1     5
2    13
3     6
4    12
dtype: int64
  • 브로드캐스트 가능. 하지만 str 데이터가 있으면 불가능.
import pandas as pd

data = {"store": ["shibuya", "shinjuku", "yokohama", "shibuya", "shinjuku", "shibuya"],
        "product": ["banana", "orange", "orange", "grape", "banana", "peach"],
        "price": [200, 1000, 800, 100, 250, 900],
        "quantity": [1, 2, 1, 2, 3, 2]}
df = pd.DataFrame(data)

print(df+2)  # DataFrameの要素全てに2を足す

>>>出力結果
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
〜中略〜

TypeError: can only concatenate str (not "int") to str

NumPy 의 병용

#pd/np #np/pd

  • np 함수에 Series 나 DataFrame 을 인수로 넣으면, 모든 요소에 대해 계산.
import pandas as pd
import numpy as np

data = {"apple": [6, 1, 4, 4, 8],
        "orange": [1, 4, 5, 6, 3],
        "banana": [6, 10, 9, 10, 5],
        "strawberry": [3, 4, 9, 2, 4],
        "kiwifruit": [10, 10, 1, 5, 8]
        }
df = pd.DataFrame(data)

print(df)
print()
print(np.log(df))  # Numpyの関数を用いて要素の値を対数変換

>>>出力結果
  apple  orange  banana  strawberry  kiwifruit
0      6       1       6           3         10
1      1       4      10           4         10
2      4       5       9           9          1
3      4       6      10           2          5
4      8       3       5           4          8

      apple    orange    banana  strawberry  kiwifruit
0  1.791759  0.000000  1.791759    1.098612   2.302585
1  0.000000  1.386294  2.302585    1.386294   2.302585
2  1.386294  1.609438  2.197225    2.197225   0.000000
3  1.386294  1.791759  2.302585    0.693147   1.609438
4  2.079442  1.098612  1.609438    1.386294   2.079442

요약통계량

#pd/stats

print(df.describe()) # 要約統計量

그룹화

#pd/grouping

  • 그룹화 : 특정 칼럼에 대해 같은 값을 가지는 행을 집약하는 조작
  • .groupby() 로 GroupBy 오브젝트가 반환되지만, print 로 표시는 불가.
    - 그룹화 후에 함수를 적용해서 확인 가능
    - .min(), .max() 은 문자열에도 적용 : 문자코드의 최소, 최댓값
import pandas as pd

data = {"store": ["shibuya", "shinjuku", "yokohama", "shibuya", "shinjuku", "shibuya"],
        "product": ["banana", "orange", "orange", "grape", "banana", "peach"],
        "price": [200, 1000, 800, 100, 250, 900],
        "quantity": [1, 2, 1, 2, 3, 2]}
df = pd.DataFrame(data)

print(df)
print()  # 改行
print(df.groupby('store').sum())  # 合計
print()  # 改行
print(df.groupby('store').mean())  # 平均
print()  # 改行
print(df.groupby('store').var())  # 分散
print()  # 改行
print(df.groupby('store').min())  # 最小値
print()  # 改行
print(df.groupby('store').max())  # 最大値

>>>出力結果
     store product  price  quantity
0   shibuya  banana    200         1
1  shinjuku  orange   1000         2
2  yokohama  orange    800         1
3   shibuya   grape    100         2
4  shinjuku  banana    250         3
5   shibuya   peach    900         2

          price  quantity
store                    
shibuya    1200         5
shinjuku   1250         5
yokohama    800         1

          price  quantity
store                    
shibuya   400.0  1.666667
shinjuku  625.0  2.500000
yokohama  800.0  1.000000

             price  quantity
store                       
shibuya   190000.0  0.333333
shinjuku  281250.0  0.500000
yokohama       NaN       NaN

         product  price  quantity
store                            
shibuya   banana    100         1
shinjuku  banana    250         2
yokohama  orange    800         1

         product  price  quantity
store                            
shibuya    peach    900         2
shinjuku  orange   1000         3
yokohama  orange    800         1

복수의 DataFrame 이용

DataFrame 연결, 결합의 개요

  • 데이터를 일괄적으로 관리하는 곳은 없고, 점포정도, 회원정보, 상품정보처럼 나누어 관리하는 곳이 많음.
  • 이러한 데이터를 합쳐서 분석하는 것으로 좀 더 깊은 고찰을 할 수 있음.
  • 연결과 결합
    - 연결 : 인덱스 방향이나 칼럼 방향으로 DataFrame 을 잇는 것.
    - 결합 : key 라는 특정 칼럼을 기준으로 DataFrame 을 잇는 것.

연결

#pd/concat

  • 인덱스나 칼럼이 일치하지 않는 부분은 NaN 표시.
import pandas as pd

data1 = {"store": ["shibuya", "shinjuku", "yokohama", "shibuya"],  # data2よりインデックスの数が1つ多い
         "product": ["banana", "orange", "orange", "grape"],
         "price": [200, 1000, 800, 1000],
         "quantity": [1, 2, 1, 1]}

data2 = {"store": ["shibuya", "shinjuku", "shibuya"],  # data1にはないカラム
         "product": ["grape", "banana", "peach"],
         "価格": [100, 250, 900],
         "quantity": [2, 3, 2]}

df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
print(pd.concat([df1, df2], axis=0))
print()  # 改行
print(pd.concat([df1, df2], axis=1))
print()  # 改行
print(pd.concat([df1, df2], axis=0).dtypes)

>>>出力結果
     store product   price  quantity     価格
0   shibuya  banana   200.0         1    NaN
1  shinjuku  orange  1000.0         2    NaN
2  yokohama  orange   800.0         1    NaN
3   shibuya   grape  1000.0         1    NaN
0   shibuya   grape     NaN         2  100.0
1  shinjuku  banana     NaN         3  250.0
2   shibuya   peach     NaN         2  900.0

      store product  price  quantity     store product     価格  quantity
0   shibuya  banana    200         1   shibuya   grape  100.0       2.0
1  shinjuku  orange   1000         2  shinjuku  banana  250.0       3.0
2  yokohama  orange    800         1   shibuya   peach  900.0       2.0
3   shibuya   grape   1000         1       NaN     NaN    NaN       NaN

store        object
product      object
price       float64
quantity      int64
価格          float64
dtype: object

결합

#pd/merge

두 데이터 프레임의 key 집합을 집합 A, B 라고 가정하면 쉬움.

  • 내부결합 : A∩B
  • 왼쪽, 오른쪽 외부결합 : 각각 집합 A, 집합 B
  • 완전 외부결합 : A∪B

교집합

import pandas as pd
pd.merge(左のDataFrame, 右のDataFrame, on = "keyにするカラム名", how="inner")

집합 A

import pandas as pd
pd.merge(左のDataFrame, 右のDataFrame, on = "keyにするカラム名", how="left")

집합 B

import pandas as pd
pd.merge(左のDataFrame, 右のDataFrame, on = "keyにするカラム名", how="right")

합집합

import pandas as pd
pd.merge(左のDataFrame, 右のDataFrame, on = "keyにするカラム名", how="outer")

첨삭문제

import pandas as pd

store_data = {
    "store": ["shibuya", "shinjuku", "yokohama", "meguro", "ikebukuro"],
    "ID": [1, 2, 3, 4, 5]
}
store_df = pd.DataFrame(store_data)  # store_dfを作成

data = {"ID": [1, 2, 3, 3, 2, 1],
        "product": ["banana", "orange", "orange", "grape", "banana", "peach"],
        "price": [200, 1000, 800, 100, 250, 900],
        "quantity": [1, 2, 1, 2, 3, 2]}
df = pd.DataFrame(data)  # dfを作成

print(df)  # dfを出力
print()
print(store_df)  # store_dfを出力
print()

  
# 問題1
# dfのインデックスが0から4までの要素、カラム名を出力してください。
df_1 = df.head()
print(df_1)
print()

  
# 問題2
# df とstore_dfをkeyをIDとして外部結合してください。
df_2 = pd.merge(df, store_df, on='ID', how = 'outer')
print(df_2)
print()

  
# 問題3
# df とstore_dfをkeyをIDとして内部結合してください。
df_3 = pd.merge(df, store_df, on='ID', how = 'inner')
print(df_3)
print()

  
# 問題4
# 問題3の回答にて作成したdf_3とgroupbyメソッドを用いてstore毎のID、price、quantityの平均値を出力してください。
df_4 = df_3.groupby('store').mean()
print(df_4)
print()

  
# 問題5
# 問題3の回答にて作成したdf_3とdiscribeメソッドを用いてID、price、quantityの要約統計量を出力してください。
df_5 = df_3.describe()
print(df_5)
profile
개발자 전직을 향해 나아가고 있는 Technical Sales Engineer

0개의 댓글