Socialify

Folder ..

Viewing frame-methods.md
328 lines (219 loc) • 13.1 KB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# Frame Methods

There are some methods attached to the frame class. You can tap into those methods by using the dot (.) notation. Most of the methods are [chainable](../chaining-methods).

### `data()` ![](https://img.shields.io/badge/chainable-green.svg?style=plastic)

The `data()` method is used to modify the data in the frame. It takes the same data argument as the constructor – which is an array of arrays.

> **Note:** If you use data method without passing any argument, it will simply return the frame.

```js
const data = [[...], [...], ...];
const frame = new Frame();
frame.data(data);

// modify the data
const newData = [[...], [...], [...], [...], ...];
frame.data(newData);
```

### `header()` ![](https://img.shields.io/badge/chainable-green.svg?style=plastic)

The `header()` method is used to modify the column names in the frame. It takes the same header argument as the constructor – which is an array of strings.

> **Note:** If you use header method without passing any argument, it will reset the column names to default header (Remember: header is optional).

```js
// modify the header
const newHeader = [...];
frame.header(newHeader);

// Reset the header to default
frame.header();

// You can use any empty value to reset the header to default, for example:
// frame.header('');
// frame.header(null);
// frame.header(undefined);
// frame.header([]);
```

### `title()` ![](https://img.shields.io/badge/chainable-green.svg?style=plastic)

The `title()` method is used to modify the title of the frame. Title is optional.

> **Note:** If you use title method without passing any argument, it will reset the title to default title (Remember: title is optional).

```js
// modify the title
const newTitle = 'New Title';
frame.title(newTitle);

// Reset the title to default
frame.title();
```

### `column()` ![](https://img.shields.io/badge/chainable-green.svg?style=plastic)

The `column()` method is used to get the column data of a particular column. It takes the column name or the index as an argument. It can also take an array of column names or indexes as an argument to get multiple columns.

The `column()` method returns a new frame with extracted column data as the data of the frame. You can chain other frame methods on the returned frame.

#### Get a single column

```js
// get a single column on Index 2 (Index starts from 0)
const column = frame.column(2);

// Alternatively, you can use the column name
const column = frame.column('Name');

// print the column
column.show();
```

#### Get multiple columns

```js
// get multiple columns on Index 2 and 3 (Index starts from 0)
const columns = frame.column([2, 3]);

// Alternatively, you can use the column names
const columns = frame.column(['Name', 'Age']);

// print the columns
columns.show();
```

### `row()` ![](https://img.shields.io/badge/chainable-green.svg?style=plastic)

The `row()` method is used to get the row data of a particular row. It takes the row index as an argument. It can also take an array of row indexes as an argument to get multiple rows.

The `row()` method returns a new frame with extracted row data as the data of the frame. You can chain other frame methods on the returned frame.

#### Get a single row

```js
// get a single row on Index 2 (Index starts from 0)
const row = frame.row(2);

// print the row
row.show();
```

#### Get multiple rows

```js
// get multiple rows on Index 2 and 3 (Index starts from 0)
const rows = frame.row([2, 3]);

// print the rows
rows.show();
```

### `fromJSON()` ![](https://img.shields.io/badge/chainable-green.svg?style=plastic)

The `fromJSON()` method is used to create a frame from a JSON object. It takes the JSON object as an argument. `fromJSON()` method returns a new frame. You can chain other frame methods on the returned frame.

> **Note:** The `fromJSON()` method does not take nested JSON objects as an argument. If you have a nested JSON object, flatten it using the `flattenJSON()` helper function first.

```js
const json = [{...}, {...}, ...];
const frame = new Frame().fromJSON(json);

// ...continue with other frame methods
```

### `fromCSV()` ![](https://img.shields.io/badge/chainable-green.svg?style=plastic)

The `fromCSV()` method is used to create a frame from a CSV string. It takes the CSV string as an argument. `fromCSV()` method returns a new frame. You can chain other frame methods on the returned frame.

> **Note:** The `fromCSV()` automatically assigns the column names from the first row of the CSV string. If you do not want to use the column names, you can use the `header()` method to assign the column names first.

```js

const path = require('path');
const csvPath = path.join(__dirname, 'data.csv');

// Sets the column names from the first row of the CSV
const frame = new Frame().fromCSV(csvPath);

// Define the column names manually
const headers = [...];

// Set the header first (important) then read the CSV
const frame = new Frame().header(headers).fromCSV(csvPath);
```

### `find()` ![](https://img.shields.io/badge/chainable-green.svg?style=plastic)

The `find()` method is used to find the rows that match the given condition. It takes a string or a number as an argument which is needed to be found in the frame. Optionally, it also takes an `options` object as as the second argument.

The valid options are defined below:

  - `row`: The row index to seach in. Can also be an array of row indexes.
  - `column`: The column name or index to search in. Can also be an array of column names or indexes.
  - `strict`: If `true`, the search will be performed on the exact value. If `false`, the search will be performed on the value as a substring. Default is `false`.

> **Hint**: You can also combine the `range()` helper method to pass a range of rows or columns.

```js
// find all the rows with value 'John' in column 'Name'
const row = frame.find('John', {column: 'Name'});

// find all the rows with value 'John' in columns 0, 1 and 2. Perform a strict search
const row = frame.find('John', {column: [0, 1, 2], strict: true});

// find all the rows with value 'John' in columns 0, 1 and 2 and rows 3, 4 and 5.
// Perform a non-strict search
const row = frame.find('John', {column: [0, 1, 2], row: [3, 4, 5], strict: false});
```

### `sort()` ![](https://img.shields.io/badge/chainable-green.svg?style=plastic)

The `sort()` method is used to sort the rows in the frame. It takes the column name or the index as an argument. It also takes an optional `ascending` or `descending` argument to sort the rows in ascending or descending order. Default is `ascending`.

> **Note**: `sort()` method permanently modifies the frame. You can chain other frame methods on the returned frame.

```js
// sort the rows in the frame with column 'Name' 
frame.sort('Name');

// sort the rows in the frame with column 'Name' in descending order
frame.sort('Name', 'descending');

// sort the rows in the frame with column index 0 in ascending order
frame.sort(0, 'ascending');
```

### `removeDuplicates()` ![](https://img.shields.io/badge/chainable-green.svg?style=plastic)

The `removeDuplicates()` method is used to remove duplicate rows from the frame. It takes a `column` argument to remove duplicate rows based on the value in the column.

  - `column`: The column name or index to remove duplicate rows based on the value in the column.

> **Note**: `removeDuplicates()` permanently modifies the frame. You can chain other frame methods on the returned frame.

```js
// remove duplicate rows based on the value in column 'Name'
frame.removeDuplicates('Name');

// remove duplicate rows based on the value in column index 0
frame.removeDuplicates(0);
```

### `head()` ![](https://img.shields.io/badge/not%20chainable-red.svg?style=plastic)

The `head()` method is used to get the first `n` rows of the frame. It takes the number of rows as an argument. If no argument is passed, it will return the first 5 rows. If the argument is greater than the number of rows in the frame, it will return the entire frame.

`head()` is a print method and it does not return a new frame and therefore it is not chainable.

```js
// get the first 5 rows
frame.head();

// get the first 10 rows
frame.head(10);
```

### `tail()` ![](https://img.shields.io/badge/not%20chainable-red.svg?style=plastic)

The `tail()` method is used to get the last `n` rows of the frame. It takes the number of rows as an argument. If no argument is passed, it will return the last 5 rows. If the argument is greater than the number of rows in the frame, it will return the entire frame.

`tail()` is a print method and it does not return a new frame and therefore it is not chainable.

```js
// get the last 5 rows
frame.tail();

// get the last 10 rows
frame.tail(10);
```

### `show()` ![](https://img.shields.io/badge/not%20chainable-red.svg?style=plastic)

The `show()` method is used to print the frame. It takes no argument.

`show()` is a print method and it does not return a new frame and therefore it is not chainable.

```js
// print the frame
frame.show();
```

### `showAll()` ![](https://img.shields.io/badge/not%20chainable-red.svg?style=plastic)

The `showAll()` method is used to print the frame with all the rows. It takes no argument.

`showAll()` is a print method and it does not return a new frame and therefore it is not chainable.

```js
// print the frame with all the rows
frame.showAll();
```

### `info()` ![](https://img.shields.io/badge/not%20chainable-red.svg?style=plastic)

The `info()` method is used to print the frame information. It takes no argument.

`info()` is a print method and it does not return a new frame and therefore it is not chainable.

```js
// print the frame information
frame.info();
```

If you run the `info()` method on the frame with [data defined in the Getting Started Section](https://github.com/luciferreeves/izuku.js/wiki/Getting-Started), it will print the following information:

```
RangeIndex: 35 elements, 0 to 34
Shape: 9 rows, 4 columns
╔═══╤═════════════╤══════════════════╤══════════════╗
║ # │ Column Name │ Types            │ Empty Values ║
╟───┼─────────────┼──────────────────┼──────────────╢
║ 0 │ Name        │ string           │ false        ║
╟───┼─────────────┼──────────────────┼──────────────╢
║ 1 │ Age         │ number,object    │ true         ║
╟───┼─────────────┼──────────────────┼──────────────╢
║ 2 │ Gender      │ string           │ false        ║
╟───┼─────────────┼──────────────────┼──────────────╢
║ 3 │ Country     │ string,undefined │ true         ║
╚═══╧═════════════╧══════════════════╧══════════════╝
Data Types: string(26), number(8), object(1), undefined(1)
Memory Usage: 550 bytes
```

### `rangeIndex()` ![](https://img.shields.io/badge/not%20chainable-red.svg?style=plastic)

The `rangeIndex()` method is used to get the element at a particular index if the complete data was flattened. It takes an index as an argument. Index is zero-based.

`rangeIndex()` does not return a new frame and therefore it is not chainable. It returns the element at the index and therefore a `console.log` is required to print the element.

```js
// get the element at index 2
console.log(frame.rangeIndex(2));
```

There are some helper methods which will make your workflow easier. Take a look at the [Helper Methods](../helper-methods) section.

### `toJSON()` ![](https://img.shields.io/badge/not%20chainable-red.svg?style=plastic)

The `toJSON()` method is used to convert the frame to a JSON file. It takes an optional path as an argument. If no argument is passed, it will save the JSON file in the current directory. It also takes an optional file name as an argument. If no argument is passed, it will save the JSON file with the name `data.json`.

```js
// save the frame as a JSON file
frame.toJSON();

// save the frame as a JSON file with the name 'frame.json' in the current directory
frame.toJSON(undefined, 'frame.json');

// save the frame as a JSON file with the name 'data.json' in the directory './data'
frame.toJSON('./data', 'data.json');
```

### `toCSV()` ![](https://img.shields.io/badge/not%20chainable-red.svg?style=plastic)

The `toCSV()` method is used to convert the frame to a CSV file. It takes an optional path as an argument. If no argument is passed, it will save the CSV file in the current directory. It also takes an optional file name as an argument. If no argument is passed, it will save the CSV file with the name `data.csv`.

```js
// save the frame as a CSV file
frame.toCSV();

// save the frame as a CSV file with the name 'frame.csv' in the current directory
frame.toCSV(undefined, 'frame.csv');

// save the frame as a CSV file with the name 'data.csv' in the directory './data'
frame.toCSV('./data', 'data.csv');
```