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
CodePudding user response:
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
CodePudding user response:
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)