Python Pandas equivalent in JavaScript

前端 未结 9 1041
时光取名叫无心
时光取名叫无心 2021-01-29 17:24

With this CSV example:

   Source,col1,col2,col3
   foo,1,2,3
   bar,3,4,5

The standard method I use Pandas is this:

  1. Parse CSV<

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-29 18:00

    Here is an dynamic approach assuming an existing header on line 1. The csv is loaded with d3.js.

    function csvToColumnArrays(csv) {
    
        var mainObj = {},
        header = Object.keys(csv[0]);
    
        for (var i = 0; i < header.length; i++) {
    
            mainObj[header[i]] = [];
        };
    
        csv.map(function(d) {
    
            for (key in mainObj) {
                mainObj[key].push(d[key])
            }
    
        });        
    
        return mainObj;
    
    }
    
    
    d3.csv(path, function(csv) {
    
        var df = csvToColumnArrays(csv);         
    
    });
    

    Then you are able to access each column of the data similar an R, python or Matlab dataframe with df.column_header[row_number].

提交回复
热议问题