-1

I'm trying to parse csv file using Papaparser but the csv file has numbers and strings as headers.papaprser parsing the numbers which are headers first and then strings.Below is my csv file.

Date,1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,Above20Net
2022-01-10,0.0,0.0,0.0,0.0,0.0,66.3,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0
2022-01-09,7.0,0.0,0.9,1.7,1.7,84.9,98.2,99.5,99.6,99.6,99.6,99.6,99.6,99.7,99.7,99.7,99.7,99.7,99.7,99.9,99.9,100.0 
pilchard
  • 12,414
  • 5
  • 11
  • 23
Swetha
  • 99
  • 1
  • 1
  • 11

1 Answers1

-1

Javascript object properties are ordered with integer keys first, this is unavoidable and not related to papaparse in particular. (see: Does JavaScript guarantee object property order?)

When parsing with {header: true} a fields property is exposed within the meta object of the result which provides an array of the columns in input order.

const csvInput = `Date,1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,Above20Net
2022-01-10,0.0,0.0,0.0,0.0,0.0,66.3,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0
2022-01-09,7.0,0.0,0.9,1.7,1.7,84.9,98.2,99.5,99.6,99.6,99.6,99.6,99.6,99.7,99.7,99.7,99.7,99.7,99.7,99.9,99.9,100.0`

const parsed = Papa.parse(csvInput, {header: true});

console.log('meta.fields: ', parsed.meta.fields);
console.log('result object: ', parsed);

// unparsing respects the original column ordering
console.log(Papa.unparse(parsed));
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.1/papaparse.min.js"></script>

Parsing with {header: false} returns an array of arrays with all elements in original input order.

const csvInput = `Date,1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,Above20Net
2022-01-10,0.0,0.0,0.0,0.0,0.0,66.3,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0
2022-01-09,7.0,0.0,0.9,1.7,1.7,84.9,98.2,99.5,99.6,99.6,99.6,99.6,99.6,99.7,99.7,99.7,99.7,99.7,99.7,99.9,99.9,100.0`

const parsed = Papa.parse(csvInput, {header: false});

console.log('result object: ', parsed);
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.1/papaparse.min.js"></script>

You might also look at d3-dsv (delimiter seperated values) which is part of the d3 library. You'll see that it parses to objects (and thus ends up sorting column keys) but it exposes a columns property on the returned data to retrieve the header columns in input order.

const csvInput = `Date,1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,Above20Net
2022-01-10,0.0,0.0,0.0,0.0,0.0,66.3,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0
2022-01-09,7.0,0.0,0.9,1.7,1.7,84.9,98.2,99.5,99.6,99.6,99.6,99.6,99.6,99.7,99.7,99.7,99.7,99.7,99.7,99.9,99.9,100.0`

const parsed = d3.csvParse(csvInput);

console.log('header columns: ', parsed.columns);
console.log('parsed data objects: ', parsed);
<script src="https://cdn.jsdelivr.net/npm/d3-dsv@3"></script>
pilchard
  • 12,414
  • 5
  • 11
  • 23