Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8771791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:52:45+00:00 2026-06-13T17:52:45+00:00

I have a dataframe which includes a column that has a list. When I

  • 0

I have a dataframe which includes a column that has a list. When I write the dataframe to a file then re-open it, I end up converting the list to a string. Is there a way to safely read/write dataframes that have lists as members?

df1 = DataFrame({'a':[['john quincy', 'tom jones', 'jerry rice'],['bob smith','sally ride','little wayne'],['seven','eight','nine'],['ten','eleven','twelve']],'b':[9,2,4,5], 'c': [7,3,0,9]})

df1.to_csv('temp.csv')
df2 = read_csv('temp.csv')

#note how the list (df1) has been converted to a string (df2)
df1['a'][0]
['john quincy', 'tom jones', 'jerry rice']

df2['a'][0]
"['john quincy', 'tom jones', 'jerry rice']"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:52:47+00:00Added an answer on June 13, 2026 at 5:52 pm

    No need to covert the list into string in the first place, list’s will be converted to string automatically. Just write the dataframe containing a list, and use ast.literal_eval on df2

                                                 a  b  c
    0   ['john quincy', 'tom jones', 'jerry rice']  9  7
    1  ['bob smith', 'sally ride', 'little wayne']  2  3
    2                   ['seven', 'eight', 'nine']  4  0
    3                  ['ten', 'eleven', 'twelve']  5  9
    
    df1.to_csv('temp.csv')
    df2 = read_csv('temp.csv')
    

    Use ast.literal_eval to get the string back to list:

    import ast
    fd2['a']=df2['a'].apply(lambda x: ast.literal_eval(x))
    type(df2['a'][1])
    

    Output:

    list
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a column in a dataframe that contains an ordered factor. I summarize
I have a dataframe called prices_df to which I add an extra column and
I have a dataframe t1 which has two fields names and rollno . I
I have several dataframes in which I want to delete each row that matches
Let's assume that we have a data frame x which contains the columns job
I have a dataframe with some numeric columns. Some row has a 0 value
I have a list of vectors which are time series of inequal length. My
I have a long list of dates that i want to parse, they are
I have a dataframe with two columns each of which represents an organism. They
I have a dataframe with 100 variables, of which I would like a subset,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.