Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example of using Tables.dictcolumntable #3387

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/src/man/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,40 @@ julia> DataFrame([(a=1, b=0), (a=2, b=0)])
2 │ 2 0
```

Sometimes your source data might have a heterogeneous set of columns for each observation.
Here is an example:

```
julia> source = [(type="circle", radius=10), (type="square", side=20)]
2-element Vector{NamedTuple{names, Tuple{String, Int64}} where names}:
(type = "circle", radius = 10)
(type = "square", side = 20)
```

If you want to create a data frame from such data containing all columns present in at least
one of the source observations, and holding `missing` entry if some column is not present then
bkamins marked this conversation as resolved.
Show resolved Hide resolved
you can use `Tables.dictcolumntable` function to help you create the desired data frame:

```
julia> DataFrame(Tables.dictcolumntable(source))
2×3 DataFrame
Row │ type radius side
│ String Int64? Int64?
─────┼──────────────────────────
1 │ circle 10 missing
2 │ square missing 20
```

The `Tables.dictcolumntable` role is to make sure that the `DataFrame` constructor gets information
bkamins marked this conversation as resolved.
Show resolved Hide resolved
about all columns present in the source data and properly instantiates them. If we did not use
this function the `DataFrame` constructor would assume that the first row of data contains the set
of columns present in the source, which would lead to an error in our example:

```
julia> DataFrame(source)
ERROR: type NamedTuple has no field radius
```

Let us finish our review of constructors by showing how to create a `DataFrame`
from a matrix. In this case you pass a matrix as a first argument. If the second
argument is just `:auto` then column names `x1`, `x2`, ... will be auto generated.
Expand Down
Loading