Skip to content

Commit 9007afe

Browse files
committed
update example to make it easier to copy-paste and understand
1 parent 2756f7a commit 9007afe

File tree

3 files changed

+93
-24
lines changed

3 files changed

+93
-24
lines changed

examples/idea-examples/movies/src/main/kotlin/org/jetbrains/kotlinx/dataframe/examples/movies/moviesWithColumnAccessor.kt

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,25 @@ package org.jetbrains.kotlinx.dataframe.examples.movies
33
import org.jetbrains.kotlinx.dataframe.DataFrame
44
import org.jetbrains.kotlinx.dataframe.api.*
55
import org.jetbrains.kotlinx.dataframe.api.column
6-
import org.jetbrains.kotlinx.dataframe.io.read
6+
import org.jetbrains.kotlinx.dataframe.io.*
77

88
private const val pathToCsv = "examples/idea-examples/movies/src/main/resources/movies.csv"
9+
// Uncomment this line if you want to copy-paste and run the code in your project without downloading the file
10+
//private const val pathToCsv = "https://raw.githubusercontent.com/Kotlin/dataframe/master/examples/idea-examples/movies/src/main/resources/movies.csv"
911

1012
fun main() {
13+
// This example shows how to use the column accessor API to address columns in different operations
14+
// https://kotlin.github.io/dataframe/apilevels.html
1115
val genres by column<String>()
1216
val title by column<String>()
1317
val year by column<Int>()
1418

15-
DataFrame
19+
/**
20+
* movieId title genres
21+
* 0 9b30aff7943f44579e92c261f3adc193 Women in Black (1997) Fantasy|Suspenseful|Comedy
22+
* 1 2a1ba1fc5caf492a80188e032995843e Bumblebee Movie (2007) Comedy|Jazz|Family|Animation
23+
*/
24+
val step1 = DataFrame
1625
.read(pathToCsv)
1726
.split { genres }.by("|").inplace()
1827
.split { title }.by {
@@ -22,12 +31,30 @@ fun main() {
2231
)
2332
}.into(title, year)
2433
.explode { genres }
25-
.filter { year() >= 0 && genres() != "(no genres listed)" }
34+
step1.print()
35+
36+
/**
37+
* Data is parsed and prepared for aggregation
38+
* movieId title year genres
39+
* 0 9b30aff7943f44579e92c261f3adc193 Women in Black 1997 Fantasy
40+
* 1 9b30aff7943f44579e92c261f3adc193 Women in Black 1997 Suspenseful
41+
* 2 9b30aff7943f44579e92c261f3adc193 Women in Black 1997 Comedy
42+
* 3 2a1ba1fc5caf492a80188e032995843e Bumblebee Movie 2007 Comedy
43+
* 4 2a1ba1fc5caf492a80188e032995843e Bumblebee Movie 2007 Jazz
44+
* 5 2a1ba1fc5caf492a80188e032995843e Bumblebee Movie 2007 Family
45+
* 6 2a1ba1fc5caf492a80188e032995843e Bumblebee Movie 2007 Animation
46+
*/
47+
48+
val step2 = step1.filter { year() >= 0 && genres() != "(no genres listed)" }
2649
.groupBy { year }
2750
.sortBy { year }
2851
.pivot(inward = false) { genres }
2952
.aggregate {
3053
count() into "count"
31-
mean() into "mean"
32-
}.print(10)
54+
title().first() into "example"
55+
}
56+
step2.print(rowsLimit = 10)
57+
58+
// Discover the final reshaped data in an interactive HTML table
59+
// step2.toStandaloneHTML().openInBrowser()
3360
}

examples/idea-examples/movies/src/main/kotlin/org/jetbrains/kotlinx/dataframe/examples/movies/moviesWithDataClass.kt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@ package org.jetbrains.kotlinx.dataframe.examples.movies
22

33
import org.jetbrains.kotlinx.dataframe.DataFrame
44
import org.jetbrains.kotlinx.dataframe.api.*
5-
import org.jetbrains.kotlinx.dataframe.io.read
5+
import org.jetbrains.kotlinx.dataframe.io.*
66

77
private const val pathToCsv = "examples/idea-examples/movies/src/main/resources/movies.csv"
8+
// Uncomment this line if you want to copy-paste and run the code in your project without downloading the file
9+
//private const val pathToCsv = "https://raw.githubusercontent.com/Kotlin/dataframe/master/examples/idea-examples/movies/src/main/resources/movies.csv"
810

911
fun main() {
10-
12+
// This example shows how to use the KProperties API to address columns in different operations
13+
// https://kotlin.github.io/dataframe/apilevels.html
1114
data class Movie(val movieId: String, val title: String, val genres: String, val year: Int)
1215

13-
DataFrame
16+
/**
17+
* movieId title genres
18+
* 0 9b30aff7943f44579e92c261f3adc193 Women in Black (1997) Fantasy|Suspenseful|Comedy
19+
* 1 2a1ba1fc5caf492a80188e032995843e Bumblebee Movie (2007) Comedy|Jazz|Family|Animation
20+
*/
21+
val step1 = DataFrame
1422
.read(pathToCsv)
1523
.split(Movie::genres).by("|").inplace()
1624
.split(Movie::title).by {
@@ -20,6 +28,19 @@ fun main() {
2028
)
2129
}.into(Movie::title, Movie::year)
2230
.explode(Movie::genres)
31+
32+
/**
33+
* Data is parsed and prepared for aggregation
34+
* movieId title year genres
35+
* 0 9b30aff7943f44579e92c261f3adc193 Women in Black 1997 Fantasy
36+
* 1 9b30aff7943f44579e92c261f3adc193 Women in Black 1997 Suspenseful
37+
* 2 9b30aff7943f44579e92c261f3adc193 Women in Black 1997 Comedy
38+
* 3 2a1ba1fc5caf492a80188e032995843e Bumblebee Movie 2007 Comedy
39+
* 4 2a1ba1fc5caf492a80188e032995843e Bumblebee Movie 2007 Jazz
40+
* 5 2a1ba1fc5caf492a80188e032995843e Bumblebee Movie 2007 Family
41+
* 6 2a1ba1fc5caf492a80188e032995843e Bumblebee Movie 2007 Animation
42+
*/
43+
val step2 = step1
2344
.filter { it[Movie::year] >= 0 && it[Movie::genres] != "(no genres listed)" }
2445
.groupBy(Movie::year)
2546
.sortBy(Movie::year)
@@ -28,4 +49,7 @@ fun main() {
2849
count() into "count"
2950
mean() into "mean"
3051
}.print(10)
52+
53+
// Discover the final reshaped data in an interactive HTML table
54+
// step2.toStandaloneHTML().openInBrowser()
3155
}

examples/idea-examples/movies/src/main/kotlin/org/jetbrains/kotlinx/dataframe/examples/movies/moviesWithInterface.kt

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,14 @@ package org.jetbrains.kotlinx.dataframe.examples.movies
22

33
import org.jetbrains.kotlinx.dataframe.DataFrame
44
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
5-
import org.jetbrains.kotlinx.dataframe.api.by
6-
import org.jetbrains.kotlinx.dataframe.api.convertTo
7-
import org.jetbrains.kotlinx.dataframe.api.count
8-
import org.jetbrains.kotlinx.dataframe.api.explode
9-
import org.jetbrains.kotlinx.dataframe.api.filter
10-
import org.jetbrains.kotlinx.dataframe.api.groupBy
11-
import org.jetbrains.kotlinx.dataframe.api.inplace
12-
import org.jetbrains.kotlinx.dataframe.api.into
13-
import org.jetbrains.kotlinx.dataframe.api.mean
14-
import org.jetbrains.kotlinx.dataframe.api.pivot
15-
import org.jetbrains.kotlinx.dataframe.api.print
16-
import org.jetbrains.kotlinx.dataframe.api.sortBy
17-
import org.jetbrains.kotlinx.dataframe.api.split
18-
import org.jetbrains.kotlinx.dataframe.io.read
5+
import org.jetbrains.kotlinx.dataframe.api.*
6+
import org.jetbrains.kotlinx.dataframe.io.*
197

8+
/**
9+
* movieId title genres
10+
* 0 9b30aff7943f44579e92c261f3adc193 Women in Black (1997) Fantasy|Suspenseful|Comedy
11+
* 1 2a1ba1fc5caf492a80188e032995843e Bumblebee Movie (2007) Comedy|Jazz|Family|Animation
12+
*/
2013
@DataSchema
2114
interface Movie {
2215
val movieId: String
@@ -25,9 +18,16 @@ interface Movie {
2518
}
2619

2720
private const val pathToCsv = "examples/idea-examples/movies/src/main/resources/movies.csv"
21+
// Uncomment this line if you want to copy-paste and run the code in your project without downloading the file
22+
//private const val pathToCsv = "https://raw.githubusercontent.com/Kotlin/dataframe/master/examples/idea-examples/movies/src/main/resources/movies.csv"
2823

2924
fun main() {
30-
DataFrame
25+
// This example shows how to the use extension properties API to address columns in different operations
26+
// https://kotlin.github.io/dataframe/apilevels.html
27+
28+
// Add the Gradle plugin and run `assemble`
29+
// check the README https://github.com/Kotlin/dataframe?tab=readme-ov-file#setup
30+
val step1 = DataFrame
3131
.read(pathToCsv).convertTo<Movie>()
3232
.split { genres }.by("|").inplace()
3333
.split { title }.by {
@@ -37,12 +37,30 @@ fun main() {
3737
)
3838
}.into("title", "year")
3939
.explode("genres")
40+
step1.print()
41+
42+
/**
43+
* Data is parsed and prepared for aggregation
44+
* movieId title year genres
45+
* 0 9b30aff7943f44579e92c261f3adc193 Women in Black 1997 Fantasy
46+
* 1 9b30aff7943f44579e92c261f3adc193 Women in Black 1997 Suspenseful
47+
* 2 9b30aff7943f44579e92c261f3adc193 Women in Black 1997 Comedy
48+
* 3 2a1ba1fc5caf492a80188e032995843e Bumblebee Movie 2007 Comedy
49+
* 4 2a1ba1fc5caf492a80188e032995843e Bumblebee Movie 2007 Jazz
50+
* 5 2a1ba1fc5caf492a80188e032995843e Bumblebee Movie 2007 Family
51+
* 6 2a1ba1fc5caf492a80188e032995843e Bumblebee Movie 2007 Animation
52+
*/
53+
val step2 = step1
4054
.filter { "year"<Int>() >= 0 && genres != "(no genres listed)" }
4155
.groupBy("year")
4256
.sortBy("year")
4357
.pivot("genres", inward = false)
4458
.aggregate {
4559
count() into "count"
4660
mean() into "mean"
47-
}.print(10)
61+
}
62+
63+
step2.print(10)
64+
// Discover the final reshaped data in an interactive HTML table
65+
// step2.toStandaloneHTML().openInBrowser()
4866
}

0 commit comments

Comments
 (0)