Python Pandas equivalent in JavaScript

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

    I've been working on a data wrangling library for JavaScript called data-forge. It's inspired by LINQ and Pandas.

    It can be installed like this:

    npm install --save data-forge
    

    Your example would work like this:

    var csvData = "Source,col1,col2,col3\n" +
        "foo,1,2,3\n" +
        "bar,3,4,5\n";
    
    var dataForge = require('data-forge');
    var dataFrame = 
        dataForge.fromCSV(csvData)
            .parseInts([ "col1", "col2", "col3" ])
            ;
    

    If your data was in a CSV file you could load it like this:

    var dataFrame = dataForge.readFileSync(fileName)
        .parseCSV()
        .parseInts([ "col1", "col2", "col3" ])
        ;
    

    You can use the select method to transform rows.

    You can extract a column using getSeries then use the select method to transform values in that column.

    You get your data back out of the data-frame like this:

    var data = dataFrame.toArray();
    

    To average a column:

     var avg = dataFrame.getSeries("col1").average();
    

    There is much more you can do with this.

    You can find more documentation on npm.

提交回复
热议问题