1

I have this list (which is created using append function twice):

['2017-04-03.csv', 108.0, '2017-04-04.csv', -12.0]

(it's much longer and length can very) and when using results = pd.DataFrame({'col':results}) I get the following:

              col
0  2017-04-03.csv
1             108
2  2017-04-04.csv
3             -12

I am looking to get:

                  col
2017-04-03.csv   108
2017-04-04.csv   -12

I checked the solutions offered here, here and here but they all not work as I expect/need. thanks

Giladbi
  • 1,822
  • 3
  • 19
  • 34
  • Can you provide a little context on how the list is created? There may be a better solution than what I've currently offered you, based on what you'e doing right now. – cs95 Mar 09 '18 at 09:19

1 Answers1

2

Slicing is your friend -

pd.DataFrame(results[1::2], index=results[::2], columns=['col'])

                  col
2017-04-03.csv  108.0
2017-04-04.csv  -12.0

Even indexed elements form the index, and odd indexed elements form the column values.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • If i have more columns, lets say 5, how can I use the same to create a split into columns? – Giladbi Mar 09 '18 at 12:24
  • @Giladbi I think it should be possible to generalise with a list comprehension... slice over [i::5] for i in range(1,5), if that makes sense. – cs95 Mar 09 '18 at 14:13
  • I didn't mange to do this. I assume my pandas knowledge is lucking. – Giladbi Mar 10 '18 at 18:45