Asmita Poddar
Asmita Poddar

Reputation: 532

Writing a data frame of mixed types to file Python Pandas

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?

Upvotes: 0

Views: 176

Answers (1)

dsanatomy
dsanatomy

Reputation: 533

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

Upvotes: 1

Related Questions