How to get the number of rows in a DataFrame in Julia

Julia is a powerful programming language for complex computational analysis in machine learning and data science.

DataFrames are 2D data structures that help organize data into a table with distinct rows and columns.

There are two ways to obtain the number of rows in a DataFrame in Julia.

  • Using the nrow() function

  • Using the size() function

Using the nrow() function

The nrow() function obtains the total number of rows present.

Syntax

nrow(dataframe)

Code

using DataFrames
df = DataFrame(student_id=[1,2,3,4,5],
name = ["Amy","Jane","John","Nancy","Peter"],
marks=[50,60,70,98,30],
age=[15,20,18,19,15])
println(nrow(df))

Explanation

  • Line 1: Imports DataFrames for use.
  • Lines 2–5: Creates a DataFrame with five rows.
  • Line 7: Prints out the number of rows in the DataFrame.

Using the size() function

The size() function helps obtain a dataset’s number of columns and rows. To get the number of rows, specify 1 inside or outside the function, as shown in the syntax below.

Syntax

size(dataframe,1)
size(dataframe)[1]

Code

using DataFrames
df = DataFrame(student_id=[1,2,3,4,5],
name = ["Amy","Jane","John","Nancy","Peter"],
marks=[50,60,70,98,30],
age=[15,20,18,19,15])
println(size(df,1))
println(size(df)[1])

Explanation

  • Lines 1–5: Import the necessary packages and create a DataFrame.
  • Lines 7–8: Print out the number of rows in a DataFrame using the size() function.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved