How to Use the Krangl Library for Data Wrangling in Kotlin

Nov 20, 2023 | Data Science

Data wrangling has become a crucial part of any data analysis task. With the Krangl library, you can seamlessly filter, transform, aggregate, and reshape your tabular data in an intuitive way. It draws inspiration from the renowned dplyr for R.

Installation

Getting started with Krangl is easy. You can add it as a dependency in your build.gradle file as follows:

repositories {
    mavenCentral()
}
dependencies {
    implementation 'com.github.holgerbrandl:krangl:0.18.4'
}

Declaring the repository is optional as Maven Central is the default.

Alternatively, you can use JitPack to build the latest snapshot:

repositories {
    maven { url 'https://jitpack.io' }
}
dependencies {
    implementation 'com.github.holgerbrandl:krangl:-SNAPSHOT'
}

To install it locally, clone the repository and run bash.gradlew install.

Features

Krangl offers a plethora of features for data manipulation, including:

  • Filter, transform, aggregate and reshape tabular data
  • Supports various data formats including CSV, TSV, JSON
  • Grouped operations and JDBC support
  • Immutability of data frames
  • Rich API inspired by dplyr, pandas, and Kotlin standard library

Examples

Let’s dive into some practical examples to see how Krangl simplifies data wrangling:

1. Reading Data

Read a data frame from a TSV file:

val iris = DataFrame.readTSV("data/iris.txt")

2. Creating Data Frames

Creating a data frame in memory:

val df: DataFrame = dataFrameOf(first_name, last_name, age, weight)(
    "Max", "Doe", 23, 55,
    "Franz", "Smith", 23, 88,
    "Horst", "Keanes", 12, 82
)

3. Adding Columns

You can add new columns using the addColumn method:

val newDF = df.addColumn(full_name) { it[first_name] + " " + it[last_name] }

4. Filtering Data

Filter data with conditions:

val filteredDF = df.filter { it[age] eq 23 }

Krangl allows you to harness the power of functional programming for data wrangling, much like how a chef prepares a gourmet dish in a kitchen. Each ingredient (data) is handled with care, transforming them through various stages (functions) to create a final masterpiece (data frame).

Troubleshooting

If you run into issues while using Krangl, consider the following troubleshooting ideas:

  • Ensure that your build.gradle file is configured correctly and you have added the necessary repositories and dependencies.
  • Check for version compatibility issues if you are using other Kotlin libraries alongside Krangl.
  • For any bugs or bugs documentation, refer to the Krangl User Guide.
  • Joining discussions on Gitter can provide additional insights or solutions to problems.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Documentation

To deepen your understanding, please refer to the following resources:

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox