pythonpython-3.xpandasdataframeread-write

When read lists from text file into pandas dataframe column face ` ParserError: Error tokenizing data. C error: Expected 16 fields in line 3, saw 21 `


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


Solution

  • 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