1

when asking a python/pandas question on stackoverflow I often like to provide a sample dataframe. I usually have a local csv file I deal with for testing.

So for a DataFrame I like to provide a code in my question like

df = pd.DataFrame()

Is there an easy way or tool to get a csv file into code in a format like this, so another user can easily recreate the dataframe?

For now I usually do it manually, which is annoying and time consuming. I have to copy/paste the data from excel to stackoverflow, remove tabs/spaces, rearrange numbers to get a list or dictionary and so on.

Example csv file:

col1 col2
1 3
2 4

I if want to provide this table I can provide code like:

d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)

I will have to create the dictionary and Dataframe manually. I manually have to write the code into the stackoverflow editor. For a more complex table this could lead to a lot of work.

Hope you get the "problem".

flywire
  • 1,155
  • 1
  • 14
  • 38
Paul
  • 252
  • 3
  • 12
  • 1
    read your csv with `pd.read_csv` and export the df to dictionary with [to_dict](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_dict.html) – Tranbi Feb 03 '23 at 10:31
  • well....that was easy...thank you – Paul Feb 03 '23 at 10:36
  • Good question. Supposed duplicate does not answer this question. – flywire Feb 27 '23 at 10:05
  • See: https://chat.stackoverflow.com/transcript/message/56032131#56032131 (using `StringIO()` and `pd.read_csv()`) and https://stackoverflow.com/a/57343119/4539999 – flywire Feb 27 '23 at 19:58

1 Answers1

1

You can make a dict from the .csv and pass it to the pandas.DataFrame constructor :

N = 5 # <- adjust here to choose the number of rows

dico = pd.read_csv("f.csv").sample(N).to_dict("list")

S = f"df = pd.DataFrame{dico}") # <- copy its output and paste in StackOverflow

You can also use pyperclip to copy directly the text you'll paste/include on your question :

#pip install pyperclip
import pyperclip

pyperclip.copy(S)
Timeless
  • 22,580
  • 4
  • 12
  • 30