1
+ const fs = require ( 'fs' )
2
+ const papa = require ( 'papaparse' )
3
+ const DF = require ( 'data-forge' ) ;
4
+ const DataManager = require ( '../dataman/data-manager.js' ) ;
5
+
6
+ class JSONtoDataFrame {
7
+
8
+ constructor ( filename ) {
9
+ const raw = fs . readFileSync ( filename , 'utf-8' )
10
+ const options = { header : true , dynamicTyping : true }
11
+ this . data = papa . parse ( raw , options ) . data
12
+ }
13
+
14
+ }
15
+
16
+ const temp = new JSONtoDataFrame ( '../../data/national_parks.csv' )
17
+ const nps = new DF . DataFrame ( temp . data )
18
+
19
+ const npsCleaned = nps
20
+ . where ( row => row . year !== "Total" )
21
+ . where ( row => row . visitors !== "NA" )
22
+ // now we can turn those columns into integers rather than strings
23
+
24
+ console . log ( npsCleaned . toString ( ) )
25
+ const typesDf = npsCleaned . detectTypes ( ) ;
26
+ //console.log(npsCleaned.detectTypes().toString())
27
+
28
+ const distinctDf = npsCleaned . dropSeries ( [ 'gnis_id' , 'geometry' , 'metadata' , 'number_of_records' , 'parkname' ,
29
+ 'region' , 'state' , 'unit_code' , 'unit_name' , 'unit_type' , 'visitors' ] )
30
+ // console.log(distinctDf.toString())
31
+
32
+ const annualVisitors = npsCleaned
33
+ . groupBy ( row => row . year )
34
+ . select ( group => ( {
35
+ // we will use the unique year values
36
+ // to populate the rows of the Year column
37
+ Year : group . first ( ) . year ,
38
+ // Now we can sum the annual visitors
39
+ Total : group . select ( row => row . visitors ) . sum ( ) ,
40
+ } ) )
41
+ . inflate ( )
42
+ console . log ( annualVisitors . toString ( ) )
0 commit comments