Reshaping a Data Frame in Julia
Last Updated :
20 Apr, 2023
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 enclosed function. We also have the default packages in Julia which are indeed used as dataframes i.e, Query.jl, DataFramesMeta.jl
Building a Dataframe in Julia
As we know the data frames are used to represent the tabular structure and store values in the columns. Each column consists of user-defined keyword arguments. Let us now start by building a basic dataframe in Julia.Â
Python3
# Julia program to create a Data Frame
# Loading Library
using DataFrames
# Creating a Data frame
data = DataFrame(A = [31, 22], B = [3.0, missing], C = ["P", "N"])
Output:
Now that we have created a DataFrame of order 2x3 which is stored in a variable called "data" and we can also perceive that the data is stored in a tabular composition. Let us understand the structured data inside the DataFrame.
Here columns A, B, and C act as keywords.
- Column "A" comprises integer values.
- Column "B" includes Float and a missing value.
- Column "C" holds a String character.
Operations on DataFrames
Now let us comprehend some of the operations in Julia.Â
Python3
# Julia program to create a Data Frame
# Loading Library
using DataFrames
# Creating a Data frame
data2 = DataFrame(A = 1:8, B = ["M", "F", "F", "M", "F", "M", "M", "F"],
C = ["T", "F", "T", "T", "F", "T", "F", "F"])
Here, another Dataframe is created and stored inside a variable "data2". 
Head Function
This operation demonstrates the head part of the DataFrameÂ
Python3
# Demonstrating Head of Data Frame
x = head(data2)
For the above DataFrame created, we have performed the head operation. It displays the topmost values in the dataframe. Output: 
Tail Function
This operation displays the tail part of the DataFrame.Â
Python3
# Demonstrating Head of Data Frame
y = tail(data2)
For the above DataFrame created we have performed the tail operation. It displays the bottom-most values in the data frame. Output: 
Row and Column operations
Python3
# Printing specific rows and columns
# Using Row and column operation
z = data2[1:4, :]
s = data2[1, :]
The above code represents the row and column operations.
- The number which is to the left of ", "(comma) represents the number of rows to be included.
- The number which is to the right of ", "(comma) represents the number of columns to be included.Â
- In the first variable(i.e, z), we are accessing the rows ranging from 1-4. The important part here is towards the right of the comma operator there's a colon(" : ") which indicates that all columns to be included.
- In the second variable(i.e, s), we are locating only the first row with all columns included.
Output:

Reshaping a DataFrame in Julia
Reshaping Dataframe includes the stack function. The data is manipulated and retrieved in a more precise form.Â
Python3
using DataFrames
data3 = DataFrame(number = [1, 2, 3, 4, 5, 6, 7, 8],
id1 = [1, 1, 2, 2, 2, 3, 3, 3],
type = ["dog", "dog", "cat", "cat",
"cat", "fish", "fish", "fish"])
In the above code, we created a dataframe having 3 columns and 8 rows with numbering given in "number" column and "id" for each "type" given (i.e, id "dog" = 1, id "cat" = 2, id "fish" = 3) in the id1 and type column respectively. Output:
Let us look at the reshaping property using the DataFrame declared above by using the "stack" function.Â
Python3
# Reshaping the Data Frame
a = stack(data3, [:type, :id1], :number)
Output:
By comparing two of the above pictures, we come to the conclusion:
- Inside the Stack function, we need to pass the variable in which our DataFrame is stored
- Hence, we are performing the reshape operation on our DataFrame created above and store in the variable "data3"
- Right after declaring the variable, leading towards the manipulating operation considering the "type" and the "id" columns displayed as strings in the row format.
- Hence repeating the number column values twice.
- We can now visualize the DataFrame is of order 16x3 after performing reshape operation using stack function.
Deleting Rows from a Data Frame
To delete rows in Julia we use a function named deleterows!(). It takes data frame name and row indices as arguments.Â
Python3
# Deleting rows from a data frame
# Calling deleterows!() Function
doc = deleterows!(data3, 6:8)
Explanation:
- As we have created a DataFrame above and stored it in a variable "data3"
- Hence, we have performed the delete operation on that respective DataFrame.
- In the above code, we have deleted the rows 6 and 7 (n : (n-1)) by using the function deleterows! and stored the result in the "doc" variable.
Output: 
Conclusion
Momentarily, we ultimately discovered what DataFrames really are in Julia and got to know all the procedures and done manipulating the data. In this article, we sophisticated mainly about reshaping the Data and the delete operation.
Similar Reads
Working with DataFrames in Julia
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 Data
7 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
Reading Tabular Data from Files in Julia
Julia is a high level, high performance, dynamic programming language which allows users to load, save, and manipulate data in various types of files for data science, analysis, and machine learning purposes. Tabular data is data that has a structure of a table and it can be easily read from various
3 min read
Indexing and Slicing Data Frames in R
Indexing and Slicing are use for accessing and manipulating data.Indexing: Accessing specific elements (rows or columns) in data structures.Slicing: Extracting subsets of data based on conditions or indices.In R, indexing a data frame allows you to retrieve specific columns by their names:dataframeN
3 min read
Data Reshaping in R Programming
Generally, in R Programming Language, data processing is done by taking data as input from a data frame where the data is organized into rows and columns. Data frames are mostly used since extracting data is much simpler and hence easier. But sometimes we need to reshape the format of the data frame
5 min read
Opening and Reading a File in Julia
File handling in Julia is achieved using functions such as open(), read(), and close(). There are many ways to read the contents of a file like readline(), readlines() and just read(). open(): To open a file existing in an absolute path, provided as the parameter.  read(): Read the contents of the f
4 min read
Handling Missing Data in Julia
Nowadays, one of the common problem of big data is to analyse the missing values in that data. Missing values can lead to some major prediction error which is not good for any business point of view. So when we encounter any missing data we have to apply different techniques to deal with missing val
4 min read
Data Munging in Julia
Data Analysis is a process using which the raw data is cleansed, transformed, and modeled in a useful way to utilize it for making decisions in business or for discovering some information. The raw data cannot be used as it is. There will be many flaws like missing values in the datasets. Moreover,
9 min read
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
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