Working with DataFrames in Julia
Last Updated :
02 Aug, 2021
A Data frame is a two-dimensional data structure that resembles a table, where the columns represent variables and rows contain values for those variables. It is mutable and can hold various data types.
Julia is a high performance, dynamic programming language which has a high-level syntax. The DataFrame package in Julia gives an ability to create and use data frames to manipulate data for data science and machine learning purposes. To do this, you must gain enough knowledge about data frames in Julia.
To know more about the Data Frame package, visit official documentation.
Â
Creating a data frame
Â
Data frame package for Julia must be installed in order to use data frames. Type the following commands in the Julia command prompt and click enter to install the data frame package:
using Pkg
Pkg.add("DataFrames")
The end of the installation process should appear like in the image shown below:
Now that you have installed the data frame package, you can create a data frame in various ways.
You can simply create a data frame using the DataFrame() function. You can mention the columns and their values in between the brackets of the DataFrame() function as the argument and run it as shown below. Before this you have to tell Julia that you are going to use data frames by using the command 'using DataFrames'.
Example 1:Â Â
python3
# Creating a data frame
using DataFrames
df = DataFrame(A = 1:5, B = ["A", "E", "I", "O", "U"],
C = ["A", "B", "C", "D", "E"])
Output:Â
Â
Â
You can also create an empty data frame and fill in the columns separately as shown below.
Example 2:Â
python3
# Creating a data frame by adding columns separately
df = Dataframe()
df.C = 1:5
df.D = ["A", "E", "I", "O", "U"]
df
Output:Â
Â
Â
Another method for creating a data frame is to add rows to an empty data frame one by one with the push!() function. You will have to declare the type of the columns before inserting the rows.
Example 3:Â
python3
# Creating a data frame by adding rows separately
df = Dataframe(E = Int[], F = String[])
push!(df, (1, "A"))
push!(df, (2, "E"))
push!(df, (3, "I"))
push!(df, (4, "O"))
push!(df, (5, "U"))
df
Output:Â
Â
Â
Accessing Rows and Columns
Now that you have created the data frame, it can be explored. We will be using the data frame which was created first with the columns 'A', 'B' and 'C'. To access the first or last few rows you can use the first(DataFrame, rows) or last(DataFrame, rows) functions respectively where 'rows' represent the number of rows you want to access.
Example 1:Â
Â
python3
# Selecting the first two rows of the data frame
first(df, 2)
Output:Â
Â
Example 2:Â
python3
# Selecting the last two rows of the data frame
last(df, 2)
Output:Â
Â
Â
To select specific number of rows or columns, you can mention the index numbers or variables of the rows or columns you want to access respectively in 'df[:, :]' as shown below.
Example 3:Â
python3
# Selecting the 3rd row of the data frame
df[:3, :]
Output:Â
Â
Example 4:Â
python3
# Selecting column 'B' of the data frame
df[:, [:B]]
Output:Â
Â
Â
Creating a subset of the data frame
To create a subset of the data frame with specific columns and number of rows you can use the select() function as shown below:Â
Example 1:Â
python3
# Creating a subset with column 'B' and
# first 3 rows of the data frame
first(select(df, :B ), 3)
Output:Â
Â
Â
You can also create a subset excluding a specific column with the select() as shown below.
Example 2:Â
python3
# Creating a subset with first 4 rows and
# excluding column 'B' of the data frame
first(select(df, Not(:B)), 4)
Output:Â
Â
Â
A subset of a data frame can also be easily created with specific rows and columns as shown below.
Example 3:Â
python3
# Creating a subset with 2nd, 3rd and 4th rows
# and 'B', 'C' columns of the data frame
df[2:4, [:B, :C]]
Output:Â
Â
Â
Modifying content of a data frame
Data can be replaced with some other data in a data frame using various functions. To perform replacement operations in a data frame you can simply use the replace!() function.Â
Example 1:Â
python3
# Replacing the number 4 with 7 in the column 'A'
replace !(df.A, 4 => 7)
df
Output:Â
Â
Â
Replacement operations on multiple columns can be performed using the broadcasting syntax which creates a subset as shown below.
Example 2:Â
python3
# Replacing the character 'E' with 'None'
# in the columns 'B' and 'C'
df[:, [:B, :C]] .= ifelse.(df[!, [:B, :C]] .== "E", "None", df[!, [:B, :C]])
df
Output:Â
Â
Â
Replacement operation for the full data frame can be performed as shown below.
Example 3:Â
python3
# Replacing the character 'A' with 'E' in the data frame
df .= ifelse.(df .== "A", "E", df)
Output:Â
Â
Â
Here every 'A' is replaced with 'E.
Â
Adding extra rows and columns to the data frame
New rows can be added to the end of a data frame by using push!() function.Â
Example 1:Â
python3
# Adding a new column to the end of the data frame
push !(df, [6 "None" "F"])
Output:Â
Â
Â
A column can be added to a specific position in a data frame by using the insert!() function.
Example 2:Â
python3
# Adding a new column to the 3rd position
# of the data frame
insert !(df, 3, ["Q", "W", "E", "R", "T", "Y"], :D)
Output:Â
Â
Â
You can also add a column by creating an array of elements you want in the column and add it to the end of the data frame as shown below.
Example 3:Â
python3
# Adding a new column in the
# last position of the data frame
arr = [2, 3, 5, 7, 11, 13]
df[:E] = arr
df
Output:Â
Â
Â
Delete rows and columns in a data frame
A specific row from a data frame can be deleted using the deleterows!() function.
Example 1:Â
python3
# Deleting the 4th row of the data frame
deleterows !(df, 4)
Output:Â
Â
Â
A column can be deleted using the deletecols!() function.
Example 2:Â
python3
# Deleting the 3rd column of the data frame
deletecols !(df, 3)
Output:Â
Â
Â
Here the column 'D', which is the 3rd column had been deleted.
Â
Merging of Data frames
Multiple data frames are created here to represent the implementation of merging operations.
Example 1:Â
python3
# Creating new data frames
df2 = DataFrame(F =["A", "E", "I", "O", "U"],
G =["G", "E", "E", "K", "S"])
df4 = DataFrame(H =["G", "R", "E", "A", "T"])
Output:Â
Â

Â
You can merge these two new data frames with the first one using the concatenating function hcat(). This function merges data frames horizontally, note that all of the data frames should contain the same number of rows when merging horizontally.
Example 2:Â
python3
# Merging the created data frames horizontally
hcat(df, df2, df4)
Output:Â
Â
Â
vcat() concatenating function can be used to merge data frames vertically, hence to represent that data frames with the same number and names of the columns as the first data frame are created.
Example 3:Â
python3
# Creating new data frames
df3 = DataFrame(A = 7, B ="O", C ="G", E = 17)
df5 = DataFrame(A = 8, B ="None", C ="H", E = 19)
# Merging the created dataframes vertically
vcat(df, df3, df5)
Output:Â
Â
Â
Basic data frames with manually entered data were used here. Many more operations can be done with many functions for CSV files containing huge amounts of data for analysis.
Similar Reads
Working with Databases in Julia
There are several ways through which data handling can be performed in Julia. Julia can be connected to a lot of databases whose connectors directly connect to Database Independent Interface (DBI) packages such as MySQL, SQLite, PostgreSQL, etc. These can be used for firing queries and obtaining the
4 min read
Working with CSV Files in Julia
CSV file (Comma-separated values file) is a plain text file that uses commas to separate values and fields. It is majorly used to store data in the form of tables or spreadsheets. Each row of a table or spreadsheet is a record filled with data that belongs to n fields (or Columns). It is used to imp
4 min read
Working with Text Files in Julia
Julia is Programming Language that is fast and dynamic in nature (most suitable for performing numerical and scientific applications) and it is optionally typed (the rich language of descriptive type and type declarations which is used to solidify our program) and general-purpose and open-sourced. J
4 min read
Working with Excel Files in Julia
Julia is a high-level open-source programming language meaning that its source is freely available. It is a language that is used to perform operations in scientific computing. Julia is used for statistical computations and data analysis. Julia provides its users with some pre-defined functions and
7 min read
Reshaping a Data Frame in Julia
DataFrame is a kind of Data Structure that holds the array of data in a tabular arrangement. We are familiar with the data frame objects and packages in python, which includes pandas, matplotlib so on, and so forth. Exactly with the equivalent approach in Julia, we use pandas.jl which operates as an
5 min read
List of Dataframes in R
DataFrames are generic data objects of R which are used to store the tabular data. They are two-dimensional, heterogeneous data structures. A list in R, however, comprises of elements, vectors, data frames, variables, or lists that may belong to different data types. In this article, we will study h
7 min read
Working with Databases in R Programming
Prerequisite: Database Connectivity with R Programming In R programming Language, a number of datasets are passed to the functions to visualize them using statistical computing. So, rather than creating datasets again and again in the console, we can pass those normalized datasets from relational da
4 min read
Sorting contents of a Data Frame in Julia
Sorting is a technique of storing data in sorted order. Sorting can be performed using sorting algorithms or sorting functions. to sort in a particular dataframe then extracting its values and applying the sorting function would easily sort the contents of a particular dataframe. Julia provides vari
6 min read
Julia - DataFrames
Data Frames in Julia is an alternative for Pandas Package in Python. Data Frames represent the data in a tabular structure. We can manipulate the data using these data frames. Various operations can be done on the Data frames for altering the data and making row-column transformations. Data Frames a
5 min read
Joining of Dataframes in R Programming
rreIn R Language, dataframes are generic data objects which are used to store the tabular data. Dataframes are considered to be the most popular data objects in R programming because it is more comfortable to analyze the data in the tabular form. Dataframes can also be taught as mattresses where eac
7 min read