简体   繁体   English

将混合类型的数据帧写入文件 Python Pandas

[英]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")我已经使用df.to_csv("dataframe.csv")将数据框写入文件

When I read the dataframe by file = pd.read_csv("dataframe.csv") , I get Col2 and Col3 as strings.当我通过file = pd.read_csv("dataframe.csv")读取 dataframe 时,我得到Col2Col3作为字符串。 However, I would like to read Col2 as a tuple, and Col3 as a list of tuples.但是,我想将Col2视为元组,将Col3视为元组列表。

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?或者,是否有另一种方法可以将数据框写入文件,以便我可以读取所需格式的列?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM