[What I want to do]
- use danfo.js to read a csv file from a url
- assign the
danfo.js
DataFrame (similar to that inpandas
) as created above to a global variable, so that I can use it somewhere else for other purpose, such as plotting charts.
[What I did] I created an async function to read the csv file:
const url =
"https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv";
async function readCSV(url) {
try {
const df = await dfd.readCSV(url);
console.log("df describe: ");
df.describe().print(); // print df describe as a table
} catch (error) {
console.error(error);
}
}
readCSV(url)
[What's the issue] It seems that the DataFrame created from reading the csv file can only be used inside this async function.
[My question] How to get the DataFrame out of the async function and assign it to a global variable?
[What I tried] I also tried the following by adding a return in the readCSV function. But this seems cumbersome because every time I want to use the DataFrame, I have to call the function to read the csv again.
async function readCSV(url) {
try {
const df = await dfd.readCSV(url);
return df;
} catch (error) {
console.error(error);
}
}
readCSV(url).then((df) => {
console.log("df head: ");
df.head().print();
});
[reference I read] How to return values from async functions using async-await from function? How do I return the response from an asynchronous call?