[QUESTION] Load multiple files from CSV?
Closed this issue · 3 comments
Ask your question
Hi, I'm new to JavaScript so apologies if this question is obvious.
I'm attempting to load two CSV files to two dataframes within the same promise. I read through your comments on Issue #50 and that was very helpful. My question is whether it's possible to load two CSV files in the same promise. If so, can you provide an example?
I'm including a link and example below of what I'm hoping to accomplish. Thanks
Additional context
I've done some reading and see that D3 loads multiple CSV's to a promise as shown below per this link : http://learnjsdata.com/read_data.html
`Promise.all([
d3.csv("/data/cities.csv"),
d3.tsv("/data/animals.tsv")
]).then(function(data) {
console.log(data[0][0]) // first row of cities
console.log(data[1][0]) // first row of animals
});`
Hi @dneagoy !
Thank you for your question.
Don't worry, a lot of users are coming from R or Python.
There is different ways to achieve that point.
Example:
The best and easier way with concurrency by using Promise.all:
const dfjs = require("dataframe-js");
Promise.all([
dfjs.DataFrame.fromCSV("/file-1.csv"),
dfjs.DataFrame.fromCSV("/file-2.csv")
]).then(dfs => {
const df = dfs.reduce((df1, df2) => df1.union(df2));
df.show();
});
Or sequentially with a new promise:
new Promise((resolve, reject) => {
dfjs.DataFrame.fromCSV("/file-1.csv")
.then(df1 => {
dfjs.DataFrame.fromCSV("/file-2.csv")
.then(df2 => resolve([df1, df2]))
.catch(reject);
})
.catch(reject);
}).then(dfs => {
const df = dfs.reduce((df1, df2) => df1.union(df2));
df.show();
});
There is of course other possibilities.
I hope it will help you.
Feel free to close if it resolved.
I also read issue #50 and find it quite helpful.
Then I come to the same issue as above. My work requires pulling in a few different tables and put them together after some operations.
My solution is not as sophisticated as above. I simply nest a few .fromCVS to get around the issue.
.fromCSV(csv no.1) {to df1;
.fromCSV(csv no.2) {to df2;
.fromCSV(csv no.3) {to df3;
[operating on df1, df2 and df3 ...]
}
}
};
sorry I did not really write the syntax but hope the above can make sense.