1

I have a list containing 2 lists that looks as the follows:

[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]

What I need to have at the end of the day, is to have 2 columns in a PandasDataFrame, the first column should hold the values found in the first list, and the second column should hold the values in the second list.

I want to iterate over each of these lists and return every single value in it into a separate column in PandasDataFrame. Where col0 in PandasDataFrame should hold the values in the first list, and the col1 in PandasDataFrame should hold values in the second list.

I am expecting output like this:

enter image description here

Is there a compact way to do so?

Thanks.

3
  • Can u add some sample input & expected ouput to get a precise solution Commented Jun 24, 2020 at 12:30
  • Can you please check the modifications @Sushanth Commented Jun 24, 2020 at 12:36
  • did you try pd.DataFrame(yourList).T ? Commented Jun 24, 2020 at 13:07

3 Answers 3

1

A simple way to do this is by converting the list into a dictionary and then creating a Data Frame from that dictionary as explained below:

l =[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]

# Creating a dictionary using dictionary comprehension
d = {'col'+str(i) : l[i] for i in range(len(l))}

df = pd.DataFrame(d)

output

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

Comments

0

As your lists are arranged rather for rows than columns, I would first build the data frame as is and then transpose it:

data = [[0, 0, 0, 0,], [1, 1, 1, 1,]]
columns = ['a', 'b', 'c', 'd']
df = pd.DataFrame(data, columns=columns)
df = df.transpose()

Comments

0

one way to do this is using transpose:

df = pd.DataFrame(yourList).T

If you want to change the columns names you can add

df = df.add_prefix('col')

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.