# Read a CSV file composed of 2 tables / nrows / skiprows

**URL:** <https://talk.observablehq.com/t/read-a-csv-file-composed-of-2-tables-nrows-skiprows/8502>\
**Category:** Help\
**Created:** [November 15, 2023, 8:45am UTC](https://talk.observablehq.com/t/read-a-csv-file-composed-of-2-tables-nrows-skiprows/8502 "2023-11-15T08:45:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scelles](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/scelles/32/8202_2.png) [@scelles](https://talk.observablehq.com/u/scelles)\
**Post date:** [November 15, 2023, 8:45am UTC](https://talk.observablehq.com/t/read-a-csv-file-composed-of-2-tables-nrows-skiprows/8502/1 "2023-11-15T08:45:08Z")

</div>

Hello,

I’d like to read this file [https://raw.githubusercontent.com/scls19fr/krp\_python\_telemetry/main/Logdata%20Essay%20mini60%202023-10-31.csv](https://raw.githubusercontent.com/scls19fr/krp_python_telemetry/main/Logdata%20Essay%20mini60%202023-10-31.csv) (ideally as 2 Danfo.js dataframes)

With Python Pandas I’m doing

```python
def load_krp_file(fname):
    nrows = 12
    df_head = pd.read_csv(fname, nrows=nrows, names=["key", "value"])
    df_head = df_head.set_index("key")
    df = pd.read_csv(fname, skiprows=nrows)
    units = df.iloc[0, :]
    units.name = "units"
    cols = df.columns
    df = df.drop(0)
    for col in df.columns:
        df[col] = df[col].astype(float)
    df["Lap"] = ((df["Distance"] - df["Distance"].shift()) < 0).astype(int).cumsum()
    #df["Laptime"] = pd.NaT
    df["Starttime"] = np.where((df["Distance"] - df["Distance"].shift()) < 0, df["Time"], np.NaN)
    #df["Starttime"].iloc[0] = 0
    df.loc[df.index[0], "Starttime"] = 0
    df["Starttime"] = df["Starttime"].ffill()
    #df["Starttime"] = df["Starttime"].fillna(method="ffill")
    df["Laptime"] = df["Time"] - df["Starttime"]

    df["Time"] = pd.to_datetime(df["Time"], unit="s")
    #df["Time"] = pd.to_timedelta(df["Time"], unit="s")

    #df["Laptime"] = pd.to_datetime(df["Laptime"], unit="s")
    
    df = df.set_index("Time")
    laps = df["Lap"].unique()
    
    laptimes = df.groupby("Lap")["Laptime"].last()[0:-1]
    
    return df_head, units.to_frame(), df, laps, laptimes

```

unfortunately I don’t see a `nrows` option to define the number of rows to read, nor a `skiprows` option to skip a given number of rows.

What are according your JS experience the best way of reading such a file and to plot in Plotly graphs?

Kind regards

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [November 15, 2023, 12:43pm UTC](https://talk.observablehq.com/t/read-a-csv-file-composed-of-2-tables-nrows-skiprows/8502/2 "2023-11-15T12:43:59Z")

</div>

Hi @scelles, I’m not familiar with danfo.js, but does this help you get started?

```js
fetch("https://raw.githubusercontent.com/scls19fr/krp_python_telemetry/main/Logdata%20Essay%20mini60%202023-10-31.csv")
.then(response => response.text())
.then(text => {
  // Normalize line breaks and split segments by double line breaks.
  const [metaText, columns, rows] = text.replace(/\r?\n/g, "\n").split(/\n\n/);
  // Parse first meta section as csv, then convert to an object
  const meta = Object.fromEntries(d3.csvParseRows(metaText));
  // Parse the two lines from the headers segment
  const [headers, units] = d3.csvParseRows(columns);
  // Parse the rows segment into an untyped array of arrays.
  const data = d3.csvParseRows(rows);
  return {meta, headers, units, data};
})

```

---

<div class="post-metadata">

**Author:** ![scelles](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/scelles/32/8202_2.png) [@scelles](https://talk.observablehq.com/u/scelles)\
**Post date:** [November 15, 2023, 1:28pm UTC](https://talk.observablehq.com/t/read-a-csv-file-composed-of-2-tables-nrows-skiprows/8502/3 "2023-11-15T13:28:41Z")

</div>

Thanks @mootari your answer helped me !

But now I don’t know how to create a Danfo.js DataFrame.

I tried creating it from array of array (data) [Creating a DataFrame - Danfo.js](https://danfo.jsdata.org/api-reference/dataframe/creating-a-dataframe#creating-a-dataframe-from-an-array-of-array) like so

```
dfd.DataFrame(parsed["data"], {columns: parsed["headers"]})

```

but it raises

```
TypeError: this.loadArrayIntoNdframe is not a function

```

My notebook is here [KRP Telemetry / Sébastien Celles | Observable](https://observablehq.com/d/027000d5fffde230)

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [November 15, 2023, 2:43pm UTC](https://talk.observablehq.com/t/read-a-csv-file-composed-of-2-tables-nrows-skiprows/8502/4 "2023-11-15T14:43:09Z")

</div>

I haven’t look at it in detail yet, but I want to point out that you’re missing one important cell in your notebook:

```js
import {print} from "@visnup/hello-danfo-js"

```

The `print` function that you currently call is `window.print` which triggers an actual print dialog (and is not available in notebooks).
