I want to embed all content within a single ipynb file. Is there a way to include a csv file as a cell and read from that?
You can build your CSV content into a cell. You need to bookend the content of the CSV so that it can be used as Python code. I like to use a doc string for that.
In code below the first cell is between the comments, with your CSV content put there, and then in another cell you can read it in using StringIO
. StringIO allows a string object to be handled by methods that take a file. Below is based on the example in official Pandas documentation.
# FIRST CELL
a_string ='''
A, B, C
a1, b1, c1
a2, b2, c2
a3, b3, c3
'''
#------------
# IN ANOTHER CELL
import io
import pandas as pd
df = pd.read_csv(io.StringIO(a_string))
Thanks, this approach seems very reasonable.
1 Like