Reputation: 143
Guys,
I have a columns-defined DataFrame and a list like this :
df = pd.DataFrame(columns=list('ABCDEF'))
[0.25, 1, [97, 99], array([18., 16., 17.]), array([ 31., 30., 29.]), array([ 0.])]
Actually every element in this list is a column for my expected DataFrame, I would like to have something like :
A B C D E F
0.25 1 [97,99] [18., 16., 17.] [ 31., 30., 29.] [ 0.]
And I could insert the list in a while loop, namely insert one row into DataFrame, e.g. in second loop I have another row like :
[0.25, 2, [132, 134], array([17.]), array([ 29., 30., 31.]), array([15., 16.])]
A B C D E F
0.25 1 [97,99] [18., 16., 17.] [ 31., 30., 29.] [ 0.]
0.25 2 [132,134] [17.] [ 29., 30., 31.] [15., 16.]
But when I create the DataFrame, it always put element in array into one column :
0 A B C D E F
0 0.25 NaN NaN NaN NaN NaN NaN
1 1 NaN NaN NaN NaN NaN NaN
2 [97.7123918594, 99.7123918594] NaN NaN NaN NaN NaN NaN
3 [17.0, 24.0, 18.0, 16.0, 17.0] NaN NaN NaN NaN NaN NaN
4 [31.0, 30.0, 29.0] NaN NaN NaN NaN NaN NaN
5 [0.0] NaN NaN NaN NaN NaN NaN
Any way can get what I expected ? Really appreciate.
Upvotes: 0
Views: 899
Reputation: 76917
You can use .loc
In [324]: df = pd.DataFrame(columns=list('ABCDEF'))
In [325]: l1 = [0.25, 1, [97, 99], array([18., 16., 17.]), array([ 31., 30., 29.]), array([ 0.])]
In [326]: df.loc[len(df.index)] = l1
In [327]: df
Out[327]:
A B C D E F
0 0.25 1 [97, 99] [18.0, 16.0, 17.0] [31.0, 30.0, 29.0] [0.0]
However, if you have list of lists upfront, you can
pd.DataFrame([l1, l2, ..., ln], columns=list('abcdef'))
Upvotes: 2