Skip to content

Instantly share code, notes, and snippets.

@Irio
Created December 16, 2019 10:22
Show Gist options
  • Save Irio/c23af8d00768d48acecb87a41d62905e to your computer and use it in GitHub Desktop.
Save Irio/c23af8d00768d48acecb87a41d62905e to your computer and use it in GitHub Desktop.
dataframe-js #99 - Code accompanying "[FEATURE] Pivot dataframe"
// # How to run:
// 1. Save this file into a new folder as `index.js`.
// 2. Run the following:
// ```
// $ npm install dataframe-js
// $ node index.js
// ```
const { DataFrame } = require("dataframe-js");
const url =
"https://gist.githubusercontent.com/Irio/e1caceb35f640b1f9c10a05dad3c4ca1/raw/fe7f566d8d4f21a9e10276ced55032937849b417/test.csv";
DataFrame.fromCSV(url).then(df => {
console.log("## with one value, one index, and one column\n");
let pivoted = df
.groupBy("Sex")
.pivot("Embarked", values => values.stat.mean("Fare"));
pivoted.show();
// console.log("\n\n## without index, aggregates values only by columns\n");
// pivoted = df
// .groupBy()
// .pivot("Embarked", values => values.stat.mean("Fare"));
// pivoted.show();
// console.log("\n\n## without columns, aggregates values only by index\n");
// pivoted = df
// .groupBy("Sex")
// .pivot(undefined, values => values.stat.mean("Fare"));
// pivoted.show();
// console.log("\n\n## with multiple values, one index, and one column\n");
// pivoted = df
// .groupBy("Sex")
// .pivot("Embarked", values => [
// values.stat.mean("Fare"),
// values.stat.mean("Age")
// ]);
// pivoted.show();
// console.log("\n\n## with one value, one index, and multiple columns\n");
// pivoted = df
// .groupBy("Sex")
// .pivot(["Embarked", "Pclass"], values => values.stat.mean("Fare"));
// pivoted.show();
// console.log("\n\n## with one value, multiple index, and one column\n");
// pivoted = df
// .groupBy(["Sex", "Parch"])
// .pivot("Embarked", values => values.stat.mean("Fare"));
// pivoted.show();
// console.log("\n\n## with one value, multiple index, and multiple columns\n");
// pivoted = df
// .groupBy(["Sex", "Parch"])
// .pivot(["Embarked", "Pclass"], values => values.stat.mean("Fare"));
// pivoted.show();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment