Public
Edited
Nov 16, 2022
Insert cell
Insert cell
remoteFileUrl = "https://raw.githubusercontent.com/javascriptdata/danfojs/dev/src/danfojs-node/test/samples/titanic.csv"
Insert cell
df = dfd.readCSV(remoteFileUrl);
Insert cell
df.shape
Insert cell
df.columns
Insert cell
df.dtypes
Insert cell
displayDataFrame(df)
Insert cell
displayDataFrame(df["Survived"])
Insert cell
displayDataFrame(df.loc({ columns: ["Age", "Fare"] }))
Insert cell
{
const config = {
columns: ["Age", "Fare"]
};
const layout = {
title: {
text: "Age and fare distribution in the Titanic"
},
yaxis: {
title: "Distribution",
},
xaxis: {
title: "Features",
},
};
const div = htl.html`<div/>`;
df.plot(div).violin({ layout, config })
return div;
}
Insert cell
survivalByClass = df.groupby(["Pclass"]).agg({ Survived: "mean" })
Insert cell
displayDataFrame(survivalByClass)
Insert cell
{
const config = {
x: "Pclass",
y: "Survived_mean",
};

const layout = {
title: {
text: "Mean survival in the Titanic grouped by passenger class"
},
yaxis: {
title: "Mean survival",
},
xaxis: {
title: "Passenger class",
},
};
const div = htl.html`<div/>`;
survivalByClass.plot(div).bar({ layout, config })
return div;
}
Insert cell
Insert cell
function displayDataFrame(df) {
// A utility function to display a Danfo data frame or series using the Observable's tables.
function convertDataFrameToJson(df) {
return df.$data.map((row, i) => {
return row.reduce(
(a, v, j) => ({ ...a, [df.columns[j]]: v}),
{"": df.index[i]});
});
}

function convertSeriesToJson(df) {
return df.$data.map((row, i) => {
return {"": df.index[i], [df.columns[0]]: row}
});
}

const dataJson = df.$isSeries ? convertSeriesToJson(df) : convertDataFrameToJson(df);
return Inputs.table(dataJson);
}
Insert cell
dfd = require('https://cdn.jsdelivr.net/npm/danfojs@1.1.2/lib/bundle.min.js').catch(() => window.dfd)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more