0

I am trying to take a list of lists and transform it into a dataframe such that the dataframe has only one column and each sublist takes one row in the dataframe. Below is an image of what I have attempted, but each word within each sublist is being put in different columns.

Current dataframe

Essentially, I want a table that looks like this: How I want the dataframe to look

1
  • 2
    It would make it easier for people to answer if you could replicate a minimal example of what your problem is in the question. Commented May 7, 2020 at 10:32

2 Answers 2

1

How about something like this, using list comprehension:

import pandas as pd

data = [[1,2,3], [4,5,6]]

# list comp. loops over each list in data (i)
# then appends every element j in i to a string
# end result is one string per row
pd.DataFrame([' '.join(str(j) for j in i) for i in data], columns=['Review'])
>>>   Review
    0  1 2 3
    1  4 5 6
Sign up to request clarification or add additional context in comments.

Comments

0

Here you go.

import pandas as pd
data=[['a b'],['c d']] # assuming each sublist has reviews 

data=[ i[0] for i in data] # make one list 

df = pd.DataFrame({'review':data})
print(df)

Output:

 review
0    a b
1    c d

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.