How to get colums instead of rows #3114

Open
opened 2024-04-24 14:18:44 +00:00 by Rednas83 · 2 comments

Something like
image
Transposed to something like

{
  Colors: ['red', 'blue', 'green'],
  Animals: ['dog', 'horse', 'cat']
}

or

[
  ['Colors', 'red', 'blue', 'green'],
  ['Animals', 'dog', 'horse', 'cat']
}
Something like ![image](/attachments/4b60aed1-be58-435a-93e8-4fdacb9a3504) Transposed to something like ``` { Colors: ['red', 'blue', 'green'], Animals: ['dog', 'horse', 'cat'] } ``` or ``` [ ['Colors', 'red', 'blue', 'green'], ['Animals', 'dog', 'horse', 'cat'] } ```
2.1 KiB
Owner

You can manually transpose the data. https://docs.sheetjs.com/docs/demos/math/tensorflow#exporting-datasets-to-a-worksheet shows an example that creates a copy of the array of arrays:

var wb = XLSX.read("Colors,Animals\nred,dog\nblue,horse\ngreen,cat", {type:"string"});
var ws = wb.Sheets.Sheet1;
var data = XLSX.utils.sheet_to_json(ws, { header: 1 });

var aoa = [];
for(var i = 0; i < data.length; ++i) {
  for(var j = 0; j < data[i].length; ++j) {
    if(!aoa[j]) aoa[j] = [];
    aoa[j][i] = data[i][j];
  }
}

This gives you the second form. The first form can be recovered with Object.fromEntries:

var object = Object.fromEntries(aoa.map(r => [r[0], r.slice(1)]))
You can manually transpose the data. https://docs.sheetjs.com/docs/demos/math/tensorflow#exporting-datasets-to-a-worksheet shows an example that creates a copy of the array of arrays: ```js var wb = XLSX.read("Colors,Animals\nred,dog\nblue,horse\ngreen,cat", {type:"string"}); var ws = wb.Sheets.Sheet1; var data = XLSX.utils.sheet_to_json(ws, { header: 1 }); var aoa = []; for(var i = 0; i < data.length; ++i) { for(var j = 0; j < data[i].length; ++j) { if(!aoa[j]) aoa[j] = []; aoa[j][i] = data[i][j]; } } ``` This gives you the second form. The first form can be recovered with `Object.fromEntries`: ```js var object = Object.fromEntries(aoa.map(r => [r[0], r.slice(1)])) ```
Author

Thanks, that worked!

I also rewrote it with map instead of for.

const data = XLSX.utils.sheet_to_json(ws, { header: 1 });
const columns = data[0] as string[]
const lists= Object.fromEntries(
  Object.keys(columns)
  .map((c) => data.map((r: any) => r[c])) // Transpose
  .map((r) => [r[0], r.slice(1)]) // Split into key and values
)

Returning
image

Perhaps add a transpose option to support it natively?

Thanks, that worked! I also rewrote it with `map` instead of `for`. ``` const data = XLSX.utils.sheet_to_json(ws, { header: 1 }); const columns = data[0] as string[] const lists= Object.fromEntries( Object.keys(columns) .map((c) => data.map((r: any) => r[c])) // Transpose .map((r) => [r[0], r.slice(1)]) // Split into key and values ) ``` Returning ![image](/attachments/d9123e6d-0d0f-4a6d-b91b-033226dcb2c1) Perhaps add a transpose option to support it natively?
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#3114
No description provided.