Python Pandas equivalent in JavaScript

前端 未结 9 1024
时光取名叫无心
时光取名叫无心 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:15

    I think the closest thing are libraries like:

    • ReclineJS
    • Miso Project Dataset

    Recline in particular has a Dataset object with a structure somewhat similar to Pandas data frames. It then allows you to connect your data with "Views" such as a data grid, graphing, maps etc. Views are usually thin wrappers around existing best of breed visualization libraries such as D3, Flot, SlickGrid etc.

    Here's an example for Recline:

    // Load some data
    var dataset = recline.Model.Dataset({
      records: [
        { value: 1, date: '2012-08-07' },
        { value: 5, b: '2013-09-07' }
      ]
      // Load CSV data instead
      // (And Recline has support for many more data source types)
      // url: 'my-local-csv-file.csv',
      // backend: 'csv'
    });
    
    // get an element from your HTML for the viewer
    var $el = $('#data-viewer');
    
    var allInOneDataViewer = new recline.View.MultiView({
      model: dataset,
      el: $el
    });
    // Your new Data Viewer will be live!
    
    0 讨论(0)
  • 2021-01-29 18:17

    Pandas.js at the moment is an experimental library, but seems very promising it uses under the hood immutable.js and NumpPy logic, both data objects series and DataFrame are there..

    0 讨论(0)
  • 2021-01-29 18:19

    It's pretty easy to parse CSV in javascript because each line's already essentially a javascript array. If you load your csv into an array of strings (one per line) it's pretty easy to load an array of arrays with the values:

    var pivot = function(data){
        var result = [];
        for (var i = 0; i < data.length; i++){
            for (var j=0; j < data[i].length; j++){
                if (i === 0){
                    result[j] = [];
                }
                result[j][i] = data[i][j];
            }
        }
        return result;
    };
    
    var getData = function() {
        var csvString = $(".myText").val();
        var csvLines = csvString.split(/\n?$/m);
    
        var dataTable = [];
    
        for (var i = 0; i < csvLines.length; i++){
            var values;
            eval("values = [" + csvLines[i] + "]");
            dataTable[i] = values;
        }
    
        return pivot(dataTable);
    };
    

    Then getData() returns a multidimensional array of values by column.

    I've demonstrated this in a jsFiddle for you.

    Of course, you can't do it quite this easily if you don't trust the input - if there could be script in your data which eval might pick up, etc.

    0 讨论(0)
提交回复
热议问题