How to read a column of csv as dtype list using pandas?

后端 未结 5 859
名媛妹妹
名媛妹妹 2020-12-02 22:58

I have a csv file with 3 columns, wherein each row of Column 3 has list of values in it. As you can see from the following table structure

Col1,Col2,Col3
1,a         


        
5条回答
  •  悲哀的现实
    2020-12-02 23:34

    I have a different approach for this, which can be used for string representations of other data types, besides just lists.

    You can use the json library and apply json.loads() to the desired column. e.g

    import json
    df.my_column = df.my_column.apply(json.loads)
    

    For this to work, however, your input strings must be enclosed in double quotations.

提交回复
热议问题