dataframe - 跳过 Julia Dataframe 的最后 N 行

标签 dataframe julia

我在从 Julia 的 Dataframe 中删除最后 N 行时遇到问题。

N_SKIP = 3

df = DataFrame(:col1=>1:10,:col2=>21:30)
N = nrow(df)

原始示例数据框:

10×2 DataFrame
│ Row │ col1  │ col2  │
│     │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1   │ 1     │ 21    │
│ 2   │ 2     │ 22    │
│ 3   │ 3     │ 23    │
│ 4   │ 4     │ 24    │
│ 5   │ 5     │ 25    │
│ 6   │ 6     │ 26    │
│ 7   │ 7     │ 27    │
│ 8   │ 8     │ 28    │
│ 9   │ 9     │ 29    │
│ 10  │ 10    │ 30    │

我想要获取前 N - N_SKIP 行,在此示例中,ID 在 1:7 范围内的行。

我试图用N = 3实现的结果:

7×2 DataFrame
│ Row │ col1  │ col2  │
│     │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1   │ 1     │ 21    │
│ 2   │ 2     │ 22    │
│ 3   │ 3     │ 23    │
│ 4   │ 4     │ 24    │
│ 5   │ 5     │ 25    │
│ 6   │ 6     │ 26    │
│ 7   │ 7     │ 27    │

我可以使用 first(df::AbstractDataFrame, n::Integer) 并在参数中传递剩余的行数。它有效,但似乎不正确。

julia> N_SKIP = 3
julia> df = DataFrame(:col1=>1:10,:col2=>21:30)
julia> N = nrow(df)
julia> first(df,N - N_SKIP)
7×2 DataFrame
│ Row │ col1  │ col2  │
│     │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1   │ 1     │ 21    │
│ 2   │ 2     │ 22    │
│ 3   │ 3     │ 23    │
│ 4   │ 4     │ 24    │
│ 5   │ 5     │ 25    │
│ 6   │ 6     │ 26    │
│ 7   │ 7     │ 27    │

最佳答案

您可以通过三种方式来实现此目的(取决于您想要什么)。

  1. 创建一个新的数据框:
julia> df[1:end-3, :]
7×2 DataFrame
 Row │ col1   col2
     │ Int64  Int64
─────┼──────────────
   1 │     1     21
   2 │     2     22
   3 │     3     23
   4 │     4     24
   5 │     5     25
   6 │     6     26
   7 │     7     27

julia> first(df, nrow(df) - 3)
7×2 DataFrame
 Row │ col1   col2
     │ Int64  Int64
─────┼──────────────
   1 │     1     21
   2 │     2     22
   3 │     3     23
   4 │     4     24
   5 │     5     25
   6 │     6     26
   7 │     7     27
  • 创建数据框 View :
  • julia> first(df, nrow(df) - 3, view=true)
    7×2 SubDataFrame
     Row │ col1   col2
         │ Int64  Int64
    ─────┼──────────────
       1 │     1     21
       2 │     2     22
       3 │     3     23
       4 │     4     24
       5 │     5     25
       6 │     6     26
       7 │     7     27
    
    julia> @view df[1:end-3, :]
    7×2 SubDataFrame
     Row │ col1   col2
         │ Int64  Int64
    ─────┼──────────────
       1 │     1     21
       2 │     2     22
       3 │     3     23
       4 │     4     24
       5 │     5     25
       6 │     6     26
       7 │     7     27
    
  • 就地更新源数据框(或者可以使用 deleteat!,具体取决于哪种方式对您更方便):
  • julia> keepat!(df, 1:nrow(df)-3)
    7×2 DataFrame
     Row │ col1   col2
         │ Int64  Int64
    ─────┼──────────────
       1 │     1     21
       2 │     2     22
       3 │     3     23
       4 │     4     24
       5 │     5     25
       6 │     6     26
       7 │     7     27
    

    关于dataframe - 跳过 Julia Dataframe 的最后 N 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74657235/

    相关文章:

    python - Dask Dataframe - 每行中的多行

    python - 创建可用性数据框

    linux - 我可以在不创建新环境的情况下在 anaconda 上安装 julia 吗?

    jupyter-notebook - Julia Pro : import Jupiter notebook

    variables - 如何在 Julia 中恢复默认常量值

    optimization - 优化 Julia 中的连接

    python requests.text 到 pandas 数据框

    python - Pandas:从索引与另一列中的值相对应的列中选择

    python - (Python,数据帧): Add a Column and insert the nth smallest value in the row

    julia - 在最近的外部作用域中捕获一个变量