How to Create a DataFrame in Julia

To create a DataFrame in Julia:

using DataFrames

df = DataFrame(column_1 = ["value_1", "value_2", "value_3", ...],
column_2 = ["value_1", "value_2", "value_3", ...],
column_3 = ["value_1", "value_2", "value_3", ...],
...
)

Steps to Create a DataFrame in Julia from Scratch

Step 1: Install the DataFrames package

If you haven’t already done so, install the DataFrames package in Julia:

using Pkg
Pkg.add("DataFrames")

Step 2: Create a DataFrame in Julia

Let’s say that you have the following data, and your goal is to create a DataFrame based on that data:

product_idproduct_nameprice
1Oven800
2Microwave250
3Dishwasher700
4Refrigerator1400
5Toaster120

Therefore, the complete code to create the DataFrame is:

using DataFrames

df = DataFrame(product_id = [1, 2, 3, 4, 5],
product_name = ["Oven", "Microwave", "Dishwasher", "Refrigerator", "Toaster"],
price = [800, 250, 700, 1400, 120]
)

print(df)

Step 3: Run the code in Julia

Run the code in Julia, and you’ll get the following DataFrame:

product_idproduct_nameprice
1Oven800
2Microwave250
3Dishwasher700
4Refrigerator1400
5Toaster120

Calculate the maximum value using the DataFrames package

To calculate the maximum price using the DataFrames package:

using DataFrames

df = DataFrame(product_id = [1, 2, 3, 4, 5],
product_name = ["Oven", "Microwave", "Dishwasher", "Refrigerator", "Toaster"],
price = [800, 250, 700, 1400, 120]
)

max_value = maximum(df.price)

print(max_value)

Run the code and you’ll get the maximum price of 1400.

Leave a Comment