删除与移动列

Julia 中的数据操作

Katerina Zahradova

Instructor

如何选择大部分列

# 选择 chocolates 的所有列,除了 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"
...
Julia 中的数据操作

Not() 运算符

# 选择 chocolates 的所有列,除了 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
...
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 中的数据操作

Let's practice!

Julia 中的数据操作

Preparing Video For Download...