1. 程式人生 > >Julia:DataFrame簡介

Julia:DataFrame簡介

using DataFrames
showln(x) = (show(x); println())  # 這句程式碼很重要

# A DataFrame is an in-memory database
df = DataFrame(A = [2, 5], B = [e, pi], C = ["x", "y"])
showln(df)
#> 2x3 DataFrame
#> |-------|---|---------|------|
#> | Row # | A | B       | C    |
#> | 1     | 1 | 2.71828 | "xx" |
#> | 2     | 2 | 3.14159 | "xy" |

# The columns of a DataFrame can be indexed using numbers or names
showln(df[1])
#> [2,5]
showln(df[:A])
#> [2,5]

showln(df[2])
#> [2.718281828459045,3.141592653589793]
showln(df[:B])
#> [2.718281828459045,3.141592653589793]

showln(df[3])
#> String["x","y"]
showln(df[:C])
#> String["x","y"]

# The rows of a DataFrame can be indexed only by using numbers
showln(df[1, :])
#> 1x3 DataFrame
#> |-------|---|---------|------|
#> | Row # | A | B       | C    |
#> | 1     | 2 | 2.71828 | "x" |
showln(df[1:2, :])
#> 2x3 DataFrame
#> |-------|---|---------|------|
#> | Row # | A | B       | C    |
#> | 1     | 2 | 2.71828 | "x" |
#> | 2     | 5 | 3.14159 | "y" |