user3720101
user3720101

Reputation: 1495

Converting and reshaping a list into a DataFrame in Pandas

How do I convert a list(x) into a 10x3 dataframe ? I want to use the first 3 entries in the list for column names. The next three entries go in row 1, the three after that go in row 2, and so on. In other words my dataframe would be 3 columns with these three names 'Phase','Formula','Sat Indx'. Then the row 1 entries would be Calcite, CaCO3, .8409.

I tried the following but I'm getting an error:

z=DataFrame(x, columns=['Phase','Formula','Sat Indx'])

print(x)

[u'Phase          ',
 u'Formula        ',
 u'Sat Indx',
 u'Calcite        ',
 u'CaCO3          ',
 0.8409314786906652,
 u'Aragonite      ',
 u'CaCO3          ',
 0.6971616312984299,
 u'H2O(g)         ',
 u'H2O            ',
 -1.5101143330253721,
 u'CO2(g)         ',
 u'CO2            ',
 -1.5522870578743806,
 u'Gypsum         ',
 u'CaSO4:2H2O     ',
 -2.993649142404755,
 u'Anhydrite      ',
 u'CaSO4          ',
 -3.2135284668446644,
 u'Portlandite    ',
 u'Ca(OH)2        ',
 -10.738067251525967,
 u'H2(g)          ',
 u'H2             ',
 -22.6,
 u'O2(g)          ',
 u'O2             ',
 -37.98786977495807,
 u'CH4(g)         ',
 u'CH4            ',
 -66.16971681191183]

Upvotes: 3

Views: 3811

Answers (1)

joris
joris

Reputation: 139312

You should first convert the list in a more appropriate format.

One option is to convert it to a list of sublists (a sublist for each row) with a list comprehension:

In [10]: x_sublists = [x[i:i+3] for i in range(0, len(x), 3)]

In [11]: pd.DataFrame(x_sublists [1:], columns=x_sublists [0])
Out[11]: 
   Phase            Formula                Sat Indx
0  Calcite          CaCO3            0.840931478691
1  Aragonite        CaCO3            0.697161631298
2  H2O(g)           H2O              -1.51011433303
3  CO2(g)           CO2              -1.55228705787
4  Gypsum           CaSO4:2H2O        -2.9936491424
5  Anhydrite        CaSO4            -3.21352846684
6  Portlandite      Ca(OH)2          -10.7380672515
7  H2(g)            H2                        -22.6
8  O2(g)            O2                -37.987869775
9  CH4(g)           CH4              -66.1697168119

Another option is reshaping the list as a numpy array (but this has the disadvantage to leading to a column with object dtype as noted by @DSM, so to end up with the same result as above, the column should be set as float manually):

In [67]: x_reshaped = np.array(x[3:], dtype=object).reshape((-1, 3))

In [68]: df = pd.DataFrame(x_reshaped, columns=x[:3])

In [69]: df['Sat Indx'] = df['Sat Indx'].astype(float)

Upvotes: 1

Related Questions