0

I have a text file called tropical.txt that have multiple lists that looks like this

['papaya', 'mangosteen', 'banana']
[]
['coconut', 'mango']

How can I read it into a pandas dataframe such that my final output will look like this? fruits is my column name in the expected output.

  fruits
0 [apple, orange, banana]
1 []
2 [pineapple, mango]


I tried

pd.read_csv('tropical.txt')

But it's giving me a parse error

ParserError: Error tokenizing data. C error: Expected 16 fields in line 3, saw 21

2 Answers 2

1

CSV is comma-separated values, so pandas is treating every comma in your list as a column separator. You should try and change the sep argument in pd.read_csv

Sign up to request clarification or add additional context in comments.

Comments

0

df = pd.read_csv('tropical.txt', sep="\n")

After reading into the pandas dataframe, convert the string into a list.

df[0] = df[0].apply(eval)

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.