tabdat

0.0.3 • Public • Published

About

TabDat is a package for working with tabular data in CSV format. It leverages Papa Parse for the CSV to JSON conversion, and then mostly utilizes Lodash for the manipulation of the data. It is intended for users who want to easily work with tabular data in Node.js and particularly for users who may be used to working with data frames in R/Python/etc.

Contributing

This is a side project (read: low priority) of mine so any contributions are welcome - just submit a pull request on GitHub. I really would like to see a robust Node.js package that allows for working with tabular data in a way that is similar to R/Python so I am hoping to get additional contributors and to be able to really flesh this package out in a thorough manner.

Installation

npm i tabdat

Usage

The standard workflow looks like this:

const TabDat = require('tabdat')
 
TabDat.convert('/path/to/file.csv').then(data => {
  td = new TabDat.TabDat(data)
  td.sortBy(row => row.mpg)
    .filter(row => countryOfOrigin === 'USA')
    .printTable()
})

Note that all methods are chainable.

Available Methods

Convert

convert(filepath)

Converts a CSV file to JSON for further processing. filepath is a string pointing to the correct location. Returns a promise with the converted data.


Add a new column

addCol(name, userFunction)

Adds a new column to the data and populates the column via the provided userFunction. name is a string representing the name of the new column and userFunction is a function.

For example:

addCol('myNewCol', row => row.mpg * 2)

This will create a new row called myNewCol and the values in this column will be the product of the value in the mpg column and 2.


Add a new row

addRow(row)

Adds a new row to the data. row is an object.

For example:

addRow({
  countryOfOrigin: 'USA',
  mpg: 28,
  numCylinders: 6
})

Note: The keys of the object must match those that are already in the data (i.e. you can't add a new column this way, use addCol for that).


Delete a column

deleteCol(colNames)

Deletes the column(s) that are specified in the array colNames.

For example:

deleteCol(['mpg', 'numCylinders'])

Filter the data

filter(userFunction)

Filters the data based on the provided function.

For example:

filter(row => row.mpg > 30)

Print the column names to the console

printColNames()

Prints to the console an array containing all of the current column names (object keys) in the data.


Print the data as a table to the console

printTable()

Prints the data to the console in a tabular format.


Rename a column

renameCol(oldColName, newColName)

Renames the column oldColName to the name provided in newColName. Both arguments are strings.

For example:

renameCol('countryOfOrigin', 'country')

This will change the name of the column countryOfOrigin to country.


Save the data to a CSV file

save(filepath)

Saves the data in its current state to a CSV file in the location provided in the filepath argument (which is a string).

For example:

save('path/to/save/to.csv')

Get the number of rows and columns (printed to the console)

size()

Prints to the console the current number of rows/columns the data consists of.


Sort the data

sortBy(criteria)

Sorts the data by the given criteria which can either be a function or an array.

For example:

sortBy(row => row.mpg)

This will sort the data in ascending order by the values in the mpg column.

sortBy(['countryOfOrigin', 'mpg'])

This will sort the data first by countryOfOrigin and then by mpg.

Package Sidebar

Install

npm i tabdat

Weekly Downloads

1

Version

0.0.3

License

ISC

Unpacked Size

8.26 kB

Total Files

3

Last publish

Collaborators

  • mthelm85