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:
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?
CodePudding user response:
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)
CodePudding user response:
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:
CodePudding user response:
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.