API reference
DataFrame
Two-dimensional, size-mutable, potentially heterogeneous tabular data. Contains labeled axes (rows and columns).
| Name | Summary |
|---|---|
new DataFrame | Construct a DataFrame from an object of arrays, an array of objects, another Dat |
DataFrame#shape | Return a [rows, columns] tuple. |
DataFrame#columns | Return the column labels of the DataFrame. |
DataFrame#index | Return the row labels of the DataFrame. |
DataFrame#dtypes | Return a Series with the dtype of each column. |
DataFrame#head | Return the first n rows. Default n is 5. |
DataFrame#tail | Return the last n rows. |
DataFrame#describe | Generate descriptive statistics for numeric columns: count, mean, std, min, 25%, |
DataFrame#col | Select a single column by name, returning it as a Series. |
DataFrame#assign | Assign new columns to a DataFrame. Returns a new DataFrame with all existing col |
DataFrame#drop | Drop specified labels from rows or columns. |
DataFrame#rename | Rename columns using a mapping. |
DataFrame#fillna | Fill NA/NaN values. |
DataFrame#dropna | Remove missing values. |
DataFrame#groupby | Group the DataFrame by one or more columns. |
DataFrame#merge | Merge DataFrame objects with a database-style join. |
DataFrame#pivot_table | Create a spreadsheet-style pivot table as a DataFrame. |
DataFrame#melt | Unpivot a DataFrame from wide to long format. |
DataFrame#sort_values | Sort by the values along either axis. |
DataFrame#query | Query the columns of a DataFrame with a boolean expression string. |
DataFrame#evalExpr | Evaluate an expression against the DataFrame columns. Returns a Series with the |
DataFrame#corr | Compute pairwise Pearson correlation of columns. |
DataFrame#cov_matrix | Compute pairwise covariance of columns (powered by numera). |
DataFrame#to_csv | Write DataFrame to a CSV file or return as a string. |
DataFrame#to_json | Convert the DataFrame to a JSON string. |
DataFrame#T | Transpose the DataFrame (swap rows and columns). |
DataFrame#sum (axis=1) | Sum of values. When axis=0 (default) returns a column-wise record. When `axis= |
DataFrame#mean (axis=1) | Mean of values. When axis=1 returns a row-wise Series. |
DataFrame#dropna (axis=1) | Remove missing values. axis=1 drops columns (instead of rows) that contain NA |
DataFrame#concat (axis=1) | Concatenate DataFrames. axis=1 stacks them side-by-side (column-wise). Duplica |
DataFrame#to_dict | Convert the DataFrame to a dictionary. orient controls the output shape: `'dic |
DataFrame#to_string | Render the DataFrame to a tab-separated string suitable for console output. Trun |
DataFrame#convert_dtypes | Convert columns to the best possible dtype. String/object columns whose values a |
DataFrame#corr_matrix (spearman) | Compute pairwise correlation. Supports 'pearson' (default) and 'spearman' (r |
new DataFrame
#new DataFrame(data, options?)
Construct a DataFrame from an object of arrays, an array of objects, another DataFrame, or a 2D array.
Parameters
dataRecord<string, unknown[]> | object[] | DataFrame | unknown[][]- The data.
options?{ index?, columns? }- Construction options.
Returns
DataFrame
Example
import { DataFrame } from 'pandas-ts';
const df = new DataFrame({ a: [1, 2, 3], b: [4, 5, 6] });
df.shape; // => [3, 2]DataFrame#shape
#df.shape
Return a [rows, columns] tuple.
Returns
[number, number]
Example
new DataFrame({ x: [1,2,3], y: [4,5,6] }).shape; // => [3, 2]DataFrame#columns
#df.columns
Return the column labels of the DataFrame.
Returns
string[]
Example
new DataFrame({ a: [1], b: [2], c: [3] }).columns; // => ['a', 'b', 'c']DataFrame#index
#df.index
Return the row labels of the DataFrame.
Returns
unknown[]
Example
new DataFrame({ a: [1,2,3] }).index; // => [0, 1, 2]DataFrame#dtypes
#df.dtypes
Return a Series with the dtype of each column.
Returns
Record<string, DType>
Example
const df = new DataFrame({ a: [1,2], b: ['x','y'] });
df.dtypes; // => { a: DType('int64'), b: DType('object') }DataFrame#head
#df.head(n?)
Return the first n rows. Default n is 5.
Returns
DataFrame
Example
const df = new DataFrame({ a: [1,2,3,4,5,6] });
df.head(3).shape; // => [3, 1]DataFrame#tail
#df.tail(n?)
Return the last n rows.
Returns
DataFrame
Example
new DataFrame({ a: [1,2,3,4,5,6] }).tail(2).shape; // => [2, 1]DataFrame#describe
#df.describe()
Generate descriptive statistics for numeric columns: count, mean, std, min, 25%, 50%, 75%, max.
Returns
DataFrame
Example
const df = new DataFrame({ a: [1,2,3,4,5] });
df.describe().index; // => ['count','mean','std','min','25%','50%','75%','max']DataFrame#col
#df.col(name)
Select a single column by name, returning it as a Series.
Parameters
namestring- Column name.
Returns
Series
Example
const df = new DataFrame({ a: [1,2,3], b: [4,5,6] });
df.col('a').to_list(); // => [1, 2, 3]DataFrame#assign
#df.assign(cols)
Assign new columns to a DataFrame. Returns a new DataFrame with all existing columns plus any new ones.
Parameters
colsRecord<string, Series | unknown[]>- Columns to assign.
Returns
DataFrame
Example
const df = new DataFrame({ a: [1,2,3] });
df.assign({ b: df.col('a').mul(2) }).columns; // => ['a', 'b']DataFrame#drop
#df.drop(labels, options?)
Drop specified labels from rows or columns.
Parameters
labelsstring | string[]- Labels to drop.
options?{ axis? }- axis=0 (rows) or axis=1 (columns). Default 0.
Returns
DataFrame
Example
const df = new DataFrame({ a: [1,2], b: [3,4], c: [5,6] });
df.drop(['b'], { axis: 1 }).columns; // => ['a', 'c']DataFrame#rename
#df.rename(mapping)
Rename columns using a mapping.
Returns
DataFrame
Example
const df = new DataFrame({ a: [1,2], b: [3,4] });
df.rename({ a: 'alpha', b: 'beta' }).columns; // => ['alpha', 'beta']DataFrame#fillna
#df.fillna(value)
Fill NA/NaN values.
Returns
DataFrame
Example
const df = new DataFrame({ a: [1, NaN, 3] });
df.fillna(0).col('a').to_list(); // => [1, 0, 3]DataFrame#dropna
#df.dropna(options?)
Remove missing values.
Returns
DataFrame
Example
const df = new DataFrame({ a: [1, NaN, 3], b: [4, 5, NaN] });
df.dropna().shape; // => [1, 2]DataFrame#groupby
#df.groupby(by, options?)
Group the DataFrame by one or more columns.
Returns
DataFrameGroupBy
Example
const df = new DataFrame({ city: ['A','B','A'], sales: [100, 200, 150] });
df.groupby('city').sum().col('sales').to_list(); // => [250, 200]DataFrame#merge
#df.merge(right, options?)
Merge DataFrame objects with a database-style join.
Parameters
rightDataFrame- Right DataFrame to merge.
options?{ on?, how?, left_on?, right_on? }- Merge options. how defaults to 'inner'.
Returns
DataFrame
Example
const left = new DataFrame({ id: [1,2], name: ['Alice','Bob'] });
const right = new DataFrame({ id: [1,2], score: [90, 85] });
left.merge(right, { on: 'id' }).col('score').to_list(); // => [90, 85]DataFrame#pivot_table
#df.pivot_table(options)
Create a spreadsheet-style pivot table as a DataFrame.
Returns
DataFrame
Example
const df = new DataFrame({
city: ['A','A','B','B'],
year: [2023,2024,2023,2024],
sales: [100,120,200,180]
});
const pt = df.pivot_table({ index: 'city', columns: 'year', values: 'sales', aggfunc: 'sum' });
pt.shape; // => [2, 2]DataFrame#melt
#df.melt(options?)
Unpivot a DataFrame from wide to long format.
Returns
DataFrame
Example
const df = new DataFrame({ id: [1,2], jan: [100,200], feb: [110,210] });
const melted = df.melt({ id_vars: 'id', value_vars: ['jan','feb'], var_name: 'month', value_name: 'sales' });
melted.shape; // => [4, 3]DataFrame#sort_values
#df.sort_values(by, options?)
Sort by the values along either axis.
Parameters
bystring | string[]- Column(s) to sort by.
options?{ ascending? }- Sort direction.
Returns
DataFrame
Example
const df = new DataFrame({ a: [3,1,2], b: ['c','a','b'] });
df.sort_values('a').col('a').to_list(); // => [1, 2, 3]DataFrame#query
#df.query(expr)
Query the columns of a DataFrame with a boolean expression string.
Returns
DataFrame
Example
const df = new DataFrame({ a: [1,2,3,4,5], b: [10,20,30,40,50] });
df.query('a > 3').col('a').to_list(); // => [4, 5]DataFrame#evalExpr
#df.evalExpr(expr)
Evaluate an expression against the DataFrame columns. Returns a Series with the result. (Note: named evalExpr because eval is reserved in JavaScript.)
Returns
Series
Example
const df = new DataFrame({ a: [1,2,3], b: [4,5,6] });
df.evalExpr('a + b').to_list(); // => [5, 7, 9]DataFrame#corr
#df.corr(method?)
Compute pairwise Pearson correlation of columns.
Returns
DataFrame
Example
const df = new DataFrame({ a: [1,2,3,4], b: [2,4,6,8] });
df.corr().col('a').iloc(1); // => 1 (perfect correlation)DataFrame#cov_matrix
#df.cov_matrix(min_periods?, ddof?)
Compute pairwise covariance of columns (powered by numera).
Returns
DataFrame
Example
const df = new DataFrame({ a: [1,2,3,4,5] });
df.cov_matrix().col('a').iloc(0); // => 2.5DataFrame#to_csv
#df.to_csv(path?, options?)
Write DataFrame to a CSV file or return as a string.
Returns
string | void
Example
const df = new DataFrame({ a: [1,2], b: [3,4] });
df.to_csv(); // => 'a,b\n1,3\n2,4\n'DataFrame#to_json
#df.to_json(path?, options?)
Convert the DataFrame to a JSON string.
Returns
string | void
Example
new DataFrame({ a: [1,2] }).to_json();
// => '[{"a":1},{"a":2}]'DataFrame#T
#df.T
Transpose the DataFrame (swap rows and columns).
Returns
DataFrame
Example
const df = new DataFrame({ a: [1,2], b: [3,4] });
df.T.shape; // => [2, 2]DataFrame#sum (axis=1)
#df.sum(axis?)
Sum of values. When axis=0 (default) returns a column-wise record. When axis=1 returns a row-wise Series.
Returns
Record<string,number> | Series
Example
const df = new DataFrame({ a: [1,2], b: [3,4] });
df.sum(1).to_list(); // => [4, 6]DataFrame#mean (axis=1)
#df.mean(axis?)
Mean of values. When axis=1 returns a row-wise Series.
Returns
Record<string,number> | Series
Example
df.mean(1).to_list(); // => [2, 3]DataFrame#dropna (axis=1)
#df.dropna({ axis?, how?, thresh? })
Remove missing values. axis=1 drops columns (instead of rows) that contain NA values according to how/thresh.
Returns
DataFrame
Example
const df = new DataFrame({ a: [1, null], b: [3, 4] });
df.dropna({ axis: 1 }).columns; // => ['b']DataFrame#concat (axis=1)
#DataFrame.concat(dfs, { axis? })
Concatenate DataFrames. axis=1 stacks them side-by-side (column-wise). Duplicate column names are auto-suffixed with _1, _2, etc.
Returns
DataFrame
Example
const df1 = new DataFrame({ a: [1,2] });
const df2 = new DataFrame({ b: [3,4] });
DataFrame.concat([df1, df2], { axis: 1 }).columns; // => ['a','b']DataFrame#to_dict
#df.to_dict(orient?)
Convert the DataFrame to a dictionary. orient controls the output shape: 'dict' (default), 'list', 'records', 'series', 'split', 'index'.
Returns
object
Example
const df = new DataFrame({ a: [1,2], b: [3,4] });
df.to_dict('records'); // => [{ a:1, b:3 }, { a:2, b:4 }]
df.to_dict('split'); // => { index:[0,1], columns:['a','b'], data:[[1,3],[2,4]] }DataFrame#to_string
#df.to_string({ index?, max_rows?, max_cols? })
Render the DataFrame to a tab-separated string suitable for console output. Truncates to max_rows / max_cols with ... indicators.
Returns
string
Example
const df = new DataFrame({ x: [1,2,3], y: [4,5,6] });
console.log(df.to_string({ max_rows: 10 }));DataFrame#convert_dtypes
#df.convert_dtypes()
Convert columns to the best possible dtype. String/object columns whose values are all numeric strings are promoted to float64.
Returns
DataFrame
Example
const df = new DataFrame({ a: ['1','2','3'], b: ['x','y','z'] });
df.convert_dtypes().col('a').dtype.name; // => 'float64'DataFrame#corr_matrix (spearman)
#df.corr_matrix(method?, min_periods?)
Compute pairwise correlation. Supports 'pearson' (default) and 'spearman' (rank-based). Returns a square DataFrame.
Returns
DataFrame
Example
const df = new DataFrame({ a: [1,2,3,4], b: [4,3,2,1] });
df.corr_matrix('spearman').col('b').iloc(0); // => -1