Saving data back to disk
This tutorial tells use the DataFrame to save data back to diskLoading data "by Hand"
Just like with loading, we can save DataFrame objects back to disk if we want to. Copying data is plausibly useful, but where this becomes more helpful is after cleaning. For example, in a typical data set in data science, we often gather data, clean it up to remove problems, and then make a copy of that cleaned data. We can automate that process in code, then save it back, so that we can then reproduce our results later. We will be showing two examples of how to save the data we have created using our DataFrames.
The first way is possibly the easiest way to save our data, which uses the Load(text fileLocation) function to read in a preexisting file such as:
//We need the DataFrame class to load in files for Data Science operations.
use Libraries.Compute.Statistics.DataFrame
//Create a DataFrame, which is essentially a table that understands
//more information about the data that is being loaded.
DataFrame frame
//This loads the project relative to the project, so the Population.csv file in the Data folder
//If we deploy our program, this path is relative to the binary.
frame:Load("../Data/Geography/US States.csv")
And in this program, we end up manipulating the data by either removing unnecessary columns or adding more to our dataset. In order to save our modified dataset without changing the preexisting one, we can use the Save(text fileDestination) function in order to create a new copy of our new data. Here is a brief description of what our Save(text fileDestination) function does:
Function | Description | Usage |
---|---|---|
frame:Save(text fileDestination) | This function takes in a string which is the file path of where we want this new dataset to be located in our file explorer. Note that the extension of our dataset must be a CSV file. | frame:Save("..\Data\catsNew.csv") |
Here is an example of changing our loaded dataset and saving it onto a new dataset:
//We can now save this. This is not very useful in this example,
//but can be if we have data frames we have generated or transformed
frame:Save("../Data/Geography/US States.csv")
Try it Yourself: Saving a File
Press the blue run button to execute the code in the code editor. Press the red stop button to end the program. Your program will work when the console outputs "Build Successful!"
Code Example: Saving a File by Hand
The other method is to use our data creation "by hand" and save the data we made within Quorum onto our computer. To do this, let's take an example program:
//We need the DataFrame class to load in files for Data Science operations.
use Libraries.Compute.Statistics.DataFrame
use Libraries.Compute.Statistics.Columns.NumberColumn
use Libraries.Containers.Array
use Libraries.Compute.Statistics.DataFrameColumn
use Libraries.Compute.Statistics.Columns.TextColumn
use Libraries.Compute.Statistics.Columns.BooleanColumn
use Libraries.Compute.Statistics.Columns.IntegerColumn
//Create a DataFrame, which is essentially a table that understands
//more information about the data that is being loaded.
DataFrame frame
//This creates a TextColumn, which contains strings/text
TextColumn txtColumn
txtColumn:SetHeader("Names")
txtColumn:Add("Daniel")
txtColumn:Add("Yuria")
txtColumn:Add("Kristine")
txtColumn:Add("Yuri")
txtColumn:Add("Rebecca")
txtColumn:Add("Brianna")
TextColumn txtColumn1
txtColumn1:SetHeader("Favorite Ice Cream")
txtColumn1:Add("Vanilla")
txtColumn1:Add("Matcha")
txtColumn1:Add("Birthday Cake")
txtColumn1:Add("Rocky Road")
txtColumn1:Add("Chocolate")
txtColumn1:Add("Strawberry")
//This creates a NumberColumn, which contains decimal values
NumberColumn numColumn
numColumn:SetHeader("Cost")
numColumn:Add(3.40)
numColumn:Add(3.50)
numColumn:Add(4.00)
numColumn:Add(4.10)
numColumn:Add(2.80)
numColumn:Add(2.50)
//This will add the data we added into a formal column
//Adding more columns will create the comma separation similar to a CSV
frame:AddColumn(txtColumn1)
frame:AddColumn(txtColumn)
frame:AddColumn(numColumn)
//The system loaded the file, but can also output it a text value, or the console, if we want that.
output frame:ToText()
Try it Yourself: Saving a File by Hand
Press the blue run button to execute the code in the code editor. Press the red stop button to end the program. Your program will work when the console outputs "Build Successful!"
To view the whole code, we can click this link.
End of Lesson
You have reached the end of the lesson for Data Science. To view more tutorials, press the button below or you can go back to the previous lesson pages.