Xóa và di chuyển cột

Xử lý dữ liệu trong Julia

Katerina Zahradova

Instructor

Cách chọn hầu hết các cột

# Chọn mọi cột của chocolates trừ review_date
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"
...
Xử lý dữ liệu trong Julia

Toán tử Not()

# Chọn mọi cột của chocolates trừ review_date
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
...
Xử lý dữ liệu trong Julia

Lỗi tiềm ẩn với select() và not()

# Bỏ cùng một cột hai lần
select!(chocolates, Not(:review_date))

# Vài dòng sau ...

select(chocolates, Not(:review_date))
ArgumentError: column name :review_date not found in the data frame
Xử lý dữ liệu trong Julia

Xóa an toàn cột không tồn tại

# Dùng 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?
...
# Dùng regex
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?
...
Xử lý dữ liệu trong Julia

Sắp xếp lại cột

# Sô-cô-la đã chọn sẵn
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
Xử lý dữ liệu trong Julia

Di chuyển sang trái

# Di chuyển cocoa sang trái
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
...
Xử lý dữ liệu trong Julia

Di chuyển cả hai sang trái

# Di chuyển cocoa và rating sang trái
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
...
Xử lý dữ liệu trong Julia

Di chuyển sang phải

# Di chuyển company sang phải
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
...
Xử lý dữ liệu trong Julia

Di chuyển linh hoạt

# Kết hợp hai thao tác di chuyển
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
...
Xử lý dữ liệu trong Julia

Di chuyển và bỏ cùng lúc

# Sắp xếp lại và bỏ review_date
# Kết hợp hai thao tác di chuyển
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
...
Xử lý dữ liệu trong Julia

Ghi chú nhanh

Xóa cột:

# Bỏ col1 và col 2
select(df, Not([:col1, "col 2"]))

Xóa cột an toàn:

# Bỏ col1 và col 2 có thể không tồn tại
select(df, Not(r"col1"), Not(Cols(==("col 2"))))

Di chuyển cột sang trái

# Di chuyển col1 và col 2 sang trái
select(df, :col1, "col 2", :)

Di chuyển cột sang phải

# Di chuyển col1 và col 2 sang phải
select(df, Not([:col1, "col 2"]) ,:col1, "col 2")
Xử lý dữ liệu trong Julia

Ayo berlatih!

Xử lý dữ liệu trong Julia

Preparing Video For Download...