Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append python lists to DataFrame columns

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?

like image 293
user7852656 Avatar asked Jan 28 '23 01:01

user7852656


1 Answers

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
like image 180
BENY Avatar answered Mar 19 '23 01:03

BENY