열 삭제 및 이동

Julia로 하는 데이터 조작

Katerina Zahradova

Instructor

대부분의 열 선택하기

# review_date를 제외하고 chocolates의 모든 열 선택
select(chocolates, :company, :bean_origin, :REF, :cocoa,
        :company_location, :ratings, :bean_type, :bean_location)
ArgumentError: column name "ratings" not found in the data frame; 
    existing most similar names are: "rating"
...
Julia로 하는 데이터 조작

Not() 연산자

# review_date를 제외하고 chocolates의 모든 열 선택
select(chocolates, Not(:review_date))
1795×9 DataFrame 
Row company  bean_origin REF   cocoa   company_location rating  bean_type bean_location
    String   String      Int64 Float64 String31         Float64 String31? String31?
______________________________________________________________________________________
1   A. Morin Agua Grande 1876  63.0    France           3.75    Sao Tome
...
Julia로 하는 데이터 조작

select()와 not()의 함정

# 같은 열을 두 번 삭제
select!(chocolates, Not(:review_date))

# 여러 줄의 코드 이후 ...

select(chocolates, Not(:review_date))
ArgumentError: column name :review_date not found in the data frame
Julia로 하는 데이터 조작

없는 열을 안전하게 삭제하기

# Cols 사용
select(chocolates, Not(Cols(==("review_date"))))
1795×9 DataFrame 
Row company  bean_origin REF   cocoa   company_location rating  bean_type bean_location
    String   String      Int64 Float64 String31         Float64 String31? String31?
...
# 정규식 사용
select(chocolates, Not(r"review\_date"))
1795×9 DataFrame 
Row company  bean_origin REF   cocoa   company_location rating  bean_type bean_location
    String   String      Int64 Float64 String31         Float64 String31? String31?
...
Julia로 하는 데이터 조작

열 순서 재배치

# 미리 선택한 초콜릿
first(chocolates)
1795x5 DataFrame
Row  company   review_date  cocoa    rating   bean_location 
     String    Int64        Float64  Float64  String31?
__________________________________________________________
1    A. Morin  2016         63.0     3.75     Sao Tome
Julia로 하는 데이터 조작

왼쪽으로 이동

# cocoa를 왼쪽으로 이동
select(chocolates, :cocoa, :)
1795x5 DataFrame
Row  cocoa    company   review_date  rating   bean_location 
     Float64  String    Int64        Float64  String31?
__________________________________________________________
1    63.0     A. Morin  2016         3.75     Sao Tome
...
Julia로 하는 데이터 조작

왼쪽으로 모두 이동

# cocoa와 rating을 왼쪽으로 이동
select(chocolates, :cocoa, :rating, :)
1795x5 DataFrame

Row  cocoa    rating   company   review_date  bean_location 
     Float64  Float64  String    Int64        String31?
__________________________________________________________
1    63.0     3.75     A. Morin  2016         Sao Tome
...
Julia로 하는 데이터 조작

오른쪽으로 이동

# company를 오른쪽으로 이동
select(chocolates, Not(:company), :company)
1795x5 DataFrame
Row  review_date  cocoa    rating   bean_location  company
     Int64        Float64  Float64  String31?      String
__________________________________________________________
1    2016         63.0     3.75     Sao Tome       A. Morin
...
Julia로 하는 데이터 조작

모두 재배치하기

# 두 이동을 결합
select(chocolates, :cocoa, :rating, Not(:company), :company)
1795x5 DataFrame
Row cocoa    rating   bean_location  review_date  company 
    Float64  Float64  String31       Int64        String
__________________________________________________________
1   63.0     3.75     Sao Tome       2016         A. Morin
...
Julia로 하는 데이터 조작

이동과 삭제 동시에

# review_date를 삭제하며 재배치
# 두 이동을 결합
select(chocolates, :cocoa, :rating, Not([:company, :review_date]), :company)
1795x4 DataFrame
Row cocoa    rating   bean_location  company 
    Float64  Float64  String31       String
__________________________________________________________
1   63.0     3.75     Sao Tome       A. Morin
...
Julia로 하는 데이터 조작

치트시트

열 삭제:

# col1과 col 2 삭제
select(df, Not([:col1, "col 2"]))

안전하게 열 삭제:

# 존재하지 않을 수 있는 col1과 col 2 삭제
select(df, Not(r"col1"), Not(Cols(==("col 2"))))

열을 왼쪽으로 이동

# col1과 col 2를 왼쪽으로 이동
select(df, :col1, "col 2", :)

열을 오른쪽으로 이동

# col1과 col 2를 오른쪽으로 이동
select(df, Not([:col1, "col 2"]) ,:col1, "col 2")
Julia로 하는 데이터 조작

연습해 봅시다!

Julia로 하는 데이터 조작

Preparing Video For Download...