Removing Missing Values in DataFrames in Julia
Overview 1
In Julia, you can easily remove missing values using the dropmissing()
function.
Code
julia> df = DataFrame(x = ["i", missing, "k", "j"], y = [1, 2, 3, missing])
4×2 DataFrame
Row │ x y
│ String? Int64?
─────┼──────────────────
1 │ i 1
2 │ missing 2
3 │ k 3
4 │ j missing
Suppose we have a dataframe with missing values missing
as shown above.
julia> dropmissing(df, :x)
3×2 DataFrame
Row │ x y
│ String Int64?
─────┼─────────────────
1 │ i 1
2 │ k 3
3 │ j missing
julia> dropmissing(df, :y)
3×2 DataFrame
Row │ x y
│ String? Int64
─────┼────────────────
1 │ i 1
2 │ missing 2
3 │ k 3
To remove missing values from the column you want, just put the symbol of the column as an argument.
julia> dropmissing(df)
2×2 DataFrame
Row │ x y
│ String Int64
─────┼───────────────
1 │ i 1
2 │ k 3
If you want to remove all missing values from the entire dataframe, you don’t need to input any column.
Full Code
using DataFrames
df = DataFrame(x = ["i", missing, "k", "j"], y = [1, 2, 3, missing])
dropmissing(df, :x)
dropmissing(df, :y)
dropmissing(df)
Environment
- OS: Windows
- julia: v1.7.3