0

Suppose I have a dataframe

    col1        col2
0   1       John Constantine
1   2       ML Engineer
2   3       Colorado

I want to append all values in col2 to a list and each row should become a string value in the list. ex

output = ['John Constantine', 'ML Engineer' , 'Colorado']

The issue is when I use

output = df.col2 

The output becomes a dataframe again. Even when I use tolist() I get list of list

2
  • Mind posting the list you got? Commented Feb 26, 2018 at 2:01
  • I got the solution. I had to read it as utf-8. Commented Feb 26, 2018 at 2:01

4 Answers 4

2

This should do it

list(df.col2)
Sign up to request clarification or add additional context in comments.

Comments

1

tolist still work

df.col2.tolist()
Out[401]: ['JohnConstantine', 'MLEngineer', 'Colorado']

Comments

0

tolist() should work:

In [14]: df = pandas.DataFrame({
    "col1": [1, 2, 3], 
    "col2": ["John Constantine", "ML Engineer", "Colorado"]
})

In [15]: df.col2.tolist()
Out[15]: ['John Constantine', 'ML Engineer', 'Colorado']

Comments

0

I think you need,

df["col2"].tolist()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.