python - list to string in df.to-csv() -
I have a data frame containing a column in which there is a list when I write dataframe in a file, then reopen it, I Finish changing the list to string. Is there any way to safely read / write dataframes with lists as a member?
df1 = DataFrame ({'a': [['John Kuni', 'Tom Jones',' Jerry Rice '' '' 'Bob Smith' ',' Sally Ride ',' Small [9, 2, 4, 5], 'C', ['seven', 'eight', 'nine'], ['ten', 'eleven', 'twelve']], 'b' : [7,3, 0, 0]} df1.to_csv ('temp.csv') df2 = read_csv ('temp.csv') # Note how to list (DF1) string (DF2) DF1 ['A' '] [0] [' John Quincy ',' Tom Jones ',' Jerry Rice '] is converted to DF2 [' A '] [0] "[' John Quincy ',' Tom Jones ',' Jerry Rice '] "
There is no need to hide, the list will automatically be converted into a string, just type a list containing dataframe, and ast.literal_eval on df2 abc 0 [[John quincy ',' tom jones] ',' Jerry Rice '] 9 7 1 [' Bob Smith ',' Sally Ride ',' Chhota Rasta '] 2 3 2 ['Seven', 'eight', 'nine'] 4 3 3 ['ten' 'eleven', 'twelve'] 59 df1.to_csv ('temp.csv') df2 = read_csv ('temp.csv') Use to retrieve ast.literal_eval string in the list: import fd2 ['a'] = df2 ['a']. Applicable (Lambda x: ast.literal_eval (x)) Type (Df2 ['a'] [1]) Output:
list
Comments
Post a Comment