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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read