0

I have a data frame which looks like this:

Col1, Col 2, Col3
'abc', (1,2), [(1,2), (3,4)]
'xyz', (3,4), [(1,2), (3,4), (5,6)]

I have written the data frame to file using df.to_csv("dataframe.csv")

When I read the dataframe by file = pd.read_csv("dataframe.csv"), I get Col2 and Col3 as strings. However, I would like to read Col2 as a tuple, and Col3 as a list of tuples.

How can I do that? Alternatively, is there another way that I should write the data frame to file, so that I can read the columns in the desired formats?

Asmita Poddar
  • 524
  • 1
  • 11
  • 27
  • 2
    Does this answer your question? [Reading back tuples from a csv file with pandas](https://stackoverflow.com/questions/23661583/reading-back-tuples-from-a-csv-file-with-pandas) – ggorlen May 21 '20 at 15:54
  • 2
    Try pickle. `df.to_pickle` and `pd.read_pickle` – Scott Boston May 21 '20 at 15:56

1 Answers1

1

file = pd.read_csv("f500.csv", dtype = {"Col2" : np.object, "Col3": np.object})

dsanatomy
  • 533
  • 4
  • 14