4

I have multiple python lists. But I want to collect each list into Dataframe columns.

new_list=pd.DataFrame([])

for i in range(0,4):
    new_list.append(my_list[i])

Then I get an error message, TypeError: cannot concatenate object of type "<class 'numpy.ndarray'>"; only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid.

my_list=([3.50913843e-05,1.64190123e-04, 4.19101449e-04, 4.40226697e-04, 3.11362684e-04],[4.573843e-05,6.795123e-04, 3.219e-04, 1.557897e-04, 3.11362684e-04], [7.0543e-05,1.64190123e-04, 2.154e-04, 4.40226697e-04, 3.11362684e-04])

The outcome I want is

3.50913843e-05     4.573843e-05     7.0543e-05
1.64190123e-04     6.795123e-04     1.64190123e-04
4.19101449e-04     3.219e-04     2.154e-04
4.40226697e-04     1.557897e-04     4.40226697e-04
3.11362684e-04     3.11362684e-04     3.11362684e-04

Any idea what is going wrong and how I could fix it?

1
  • can't replicate your error given your example, please update Commented Dec 4, 2018 at 23:18

2 Answers 2

6

So using

pd.DataFrame(np.array(my_list).T)
Out[929]: 
          0         1         2
0  0.000035  0.000046  0.000071
1  0.000164  0.000680  0.000164
2  0.000419  0.000322  0.000215
3  0.000440  0.000156  0.000440
4  0.000311  0.000311  0.000311
Sign up to request clarification or add additional context in comments.

1 Comment

list or array makes no difference to the final result.
2

You don't have to iteratively build a DataFrame - just pass it in at once. Either, pass a list-of-lists and transpose:

pd.DataFrame(list(my_list)).T

          0         1         2
0  0.000035  0.000046  0.000071
1  0.000164  0.000680  0.000164
2  0.000419  0.000322  0.000215
3  0.000440  0.000156  0.000440
4  0.000311  0.000311  0.000311

Or, inverse-zip, without the transposition.

pd.DataFrame(list(zip(*my_list)))

          0         1         2
0  0.000035  0.000046  0.000071
1  0.000164  0.000680  0.000164
2  0.000419  0.000322  0.000215
3  0.000440  0.000156  0.000440
4  0.000311  0.000311  0.000311

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.