Comments (9)
This is the intended way to do it:
julia> combine(df, [:a, :b] .=> myextrema .=> x -> x .* ["_min", "_max"])
1×4 DataFrame
Row │ a_min a_max b_min b_max
│ Int64 Int64 Int64 Int64
─────┼────────────────────────────
1 │ 1 10 4 13
You can then even do just e.g.:
julia> combine(df, [:a, :b] .=> Ref∘extrema .=> x -> x .* ["_min", "_max"])
1×4 DataFrame
Row │ a_min a_max b_min b_max
│ Int64 Int64 Int64 Int64
─────┼────────────────────────────
1 │ 1 10 4 13
from dataframes.jl.
Thank you very much - I couldn't find such an example in the documentation.
I still don't understand why your second version works 😅.
This approach has the disadvantage that one needs to replicate which fields the transformation function has. Looks flexible, and easy to understand, which is really great, but also like duplication.
from dataframes.jl.
- It is documented that to produce multiple columns you have to either pass
AsTable
or a vector of column names. - It is documented that you can auto-generate the target column names using a function (to dynamically generate them). In this case the function takes source column names as input.
This approach has the disadvantage that one needs to replicate which fields the transformation function has.
Yes - this is a disadvantage. That is why I have commented that you do not have to pass these column names in the function (the example with Ref
, which skips defining target column names).
We could allow for a function taking both "source column names" and "names returned by a function" and allowing combining them, but it seemed overly complex (i.e. the API would be hard for typical users to understand and learn). What I have given you was the most concise variant.
The variant that you want is available, and it avoids duplication, but the disadvantage is that the code is longer (so I thought that it is less interesting):
julia> using DataFrames
julia> df = DataFrame(a = 1:10, b = 4:13)
10×2 DataFrame
Row │ a b
│ Int64 Int64
─────┼──────────────
1 │ 1 4
2 │ 2 5
3 │ 3 6
4 │ 4 7
5 │ 5 8
6 │ 6 9
7 │ 7 10
8 │ 8 11
9 │ 9 12
10 │ 10 13
julia> function myextrema(a)
ex = extrema(a[1])
n = propertynames(a)[1]
(; Symbol(n, "_min") => ex[1], Symbol(n, "_max") => ex[2])
end
myextrema (generic function with 1 method)
julia>
julia> combine(df, AsTable.([:a, :b]) .=> myextrema .=> AsTable)
1×4 DataFrame
Row │ a_min a_max b_min b_max
│ Int64 Int64 Int64 Int64
─────┼────────────────────────────
1 │ 1 10 4 13
from dataframes.jl.
2. It is documented that you can auto-generate the target column names using a function (to dynamically generate them). In this case the function takes source column names as input.
Could an example be added to https://dataframes.juliadata.org/stable/man/working_with_dataframes/?
This was my source of truth and there I couldn't find it.
from dataframes.jl.
There is an example in the docstring. https://dataframes.juliadata.org/stable/lib/functions/#DataFrames.combine. We could add also something in the intro manual. Could you propose something that you would find most useful?
from dataframes.jl.
I think just below .=>
within the combine
Section would be nice
julia> combine(df, names(df) .=> sum, names(df) .=> prod)
1×4 DataFrame
Row │ A_sum B_sum A_prod B_prod
│ Int64 Float64 Int64 Float64
─────┼─────────────────────────────────
1 │ 10 10.0 24 24.0
# this is new:
julia> combine(df, names(df) .=> Ref ∘ extrema .=> (c -> c .* ["_min", "_max"]))
Probably with a little extra explanation what the Ref
is doing here (I haven't entirely understood its need yet).
from dataframes.jl.
https://bkamins.github.io/julialang/2024/03/22/minicontainers.html
from dataframes.jl.
See #3433 for an update of the manual. Of course please comment if something is not clear or should be improved.
from dataframes.jl.
looks especially good. Thank you for the detailed documentation improvement!
from dataframes.jl.
Related Issues (20)
- What is the best way to write large DataFrames efficiently and with high performance in Julia while minimizing memory usage? HOT 4
- Segmentation Fault when reading compressed file HOT 1
- Revisit spreading for `AsTable` output` HOT 6
- Better error message when forming a DataFrame from a vector of dictionaries with missing data. HOT 2
- `describe` is slow HOT 3
- CartesianIndex error in Julia 1.11 HOT 4
- `DataFrame(x=Int[], y=Int)` HOT 3
- Add comparison function for dataframes which can handle both isapprox and isequal column types HOT 2
- unique fails with column-type FixedDecimal HOT 5
- mapcols! should modify the parent of a SubDataFrame HOT 11
- Feature request: Pairs in stack HOT 2
- Grouped DataFrame with array elements fails to combine HOT 4
- error when combining a grouped empty dataframe using `first` HOT 6
- Short circuit && on subset? HOT 1
- Integer strings as colnames/selectors are error prone HOT 2
- Suggestion - Matrix Syntax for hcat (as well as vcat) HOT 4
- `join` should not introduce `Missing` types to schema HOT 1
- Consider removing Tables.allocatecolumn in vcat
- DataFrame(t::Table) converts PooledVector columns HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dataframes.jl.