官术网_书友最值得收藏!

  • Julia Cookbook
  • Jalem Raj Rohit
  • 232字
  • 2021-07-08 11:12:45

Handling data with CSV files

In this section, we will explain ways in which you can handle files with the Comma-separated Values (CSV) file format.

Getting ready

Install the DataFrames package, which is the Julia package for working with data arrays and dataframes. The command for adding the DataFrames packages to the catalog is as follows:

Pkg.add("DataFrames")

Make sure that all the installed packages are up-to-date: Pkg.update()

How to do it...

CSV files, as the name suggests, are files whose contents are separated by commas. CSV files can be accessed and read into the REPL process by executing the following steps:

  1. Assign a variable to the local source directory of the file:
    s = "/Users/username/dir/iris.csv"
    
  2. The readtable() command is used to read the data from the source. The data is read in the form of a Julia DataFrame:
    iris = readtable(s)
    

Data can be written to CSV files from a Julia DataFrame using the following steps:

  1. Create a data structure with some data inside it. For example, let's create a two-dimensional dataframe to view the the process of writing files of different formats better using DataFrames:
    df = DataFrame(A = 1:10, B = 11:20)
    
    • The preceding command creates a two-dimensional dataframe with columns named A and B.
  2. Now, the dataframe created in Step 1 can be exported to an external CSV file by using the following command:
    writetable("data.csv", df)
    
主站蜘蛛池模板: 微山县| 博白县| 牙克石市| 翁源县| 都江堰市| 汤原县| 蓝山县| 泽州县| 阿巴嘎旗| 利津县| 田阳县| 榆林市| 噶尔县| 大方县| 栾川县| 六安市| 连城县| 安达市| 山西省| 平陆县| 融水| 沙湾县| 德阳市| 汝阳县| 辛集市| 汤阴县| 砚山县| 滨海县| 额敏县| 醴陵市| 遵化市| 阜新| 金堂县| 会同县| 阿坝| 苏尼特左旗| 曲阳县| 清河县| 余姚市| 江川县| 浮山县|