'Python sets stored as string in a column of a pandas dataframe

I have a pandas dataframe, where one column contains sets of strings (each row is a (single) set of strings). However, when I "save" this dataframe to csv, and read it back into a pandas dataframe later, each set of strings in this particular column seems to be saved as a single string. For example the value in this particular row, should be a single set of strings, but it seems to have been read in as a single string:

enter image description here

I need to access this data as a python set of strings, is there a way to turn this back into a set? Or better yet, have pandas read this back in as a set?



Solution 1:[1]

You can wrap the string in the "set()" function to turn it back into a set.

string = "{'+-0-', '0---', '+0+-', '0-0-', '++++', '+++0', '+++-', '+---', '0+++', '0++0', '0+00', '+-+-', '000-', '+00-'}"
new_set = set(string)

Solution 2:[2]

I think you could use a different separator while converting dataframe to csv.

import pandas as pd
df = pd.DataFrame(["{'Ramesh','Suresh','Sachin','Venkat'}"],columns=['set'])
print('Old df  \n', df)

df.to_csv('mycsv.csv', sep= ';', index=False)

new_df = pd.read_csv('mycsv.csv', sep= ';')
print('New df \n',new_df)

Output:

enter image description here

Solution 3:[3]

You can use series.apply I think:

Let's say your column of sets was called column_of_sets. Assuming you've already read the csv, now do this to convert back to sets.

df['column_of_sets'] = df['column_of_sets'].apply(eval)

I'm taking eval from @Cabara's comment. I think it is the best bet.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Michael
Solution 2 Manjunath K Mayya
Solution 3 Willow