Reputation: 615
I have a list of lists. Here's a sample of it.
[[], ['1', 'Anthony Taylor', '6', '11.17', '0.68', '0.17', '1.83', '11', '0.17', '1'], ['2', 'Jonathan Moss', '4', '12.75', '0.73', '0.00', '2.00', '8', '0.00', '0'], ['3', 'Martin Atkinson', '3', '11.00', '0.69', '0.67', '1.67', '5', '0.00', '0'], ['4', 'Graham Scott', '3', '10.00', '0.67', '0.00', '0.33', '1', '0.00', '0'], ['5', 'Chris Kavanagh', '3', '10.33', '0.74', '0.00', '1.67', '5', '0.00', '0'], ['6', 'Michael Oliver', '2', '12.00', '0.62', '1.00', '2.00', '4', '0.00', '0'], ['7', 'Paul Tierney', '2', '12.50', '0.66', '0.00', '2.50', '5', '0.00', '0'], ['8', 'Mike Dean', '2', '14.50', '0.73', '0.50', '3.00', '6', '0.00', '0'], ['9', 'Andre Marriner', '2', '8.50', '0.53', '0.00', '2.00', '4', '0.00', '0'], ['10', 'Kevin Friend', '2', '9.50', '0.50', '0.00', '3.00', '6', '0.00', '0'], ['11', 'Craig Pawson', '1', '9.00', '0.45', '0.00', '1.00', '1', '0.00', '0'], ['12', 'Stuart Attwell', '1', '9.00', '0.75', '0.00', '2.00', '2', '0.00', '0'], ['13', 'Lee Probert', '1', '11.00', '0.73', '0.00', '0.00', '0', '0.00', '0'], ['', 'Total / Average', '32', '11.09', '0.66', '0.19', '1.81', '58', '0.03', '1']]
How do I create a Pandas DataFrame out of this which is similar to this format(every row for a separate name)?
['1', 'Anthony Taylor', '6', '11.17', '0.68', '0.17', '1.83', '11', '0.17', '1']
['2', 'Jonathan Moss', '4', '12.75', '0.73', '0.00', '2.00', '8', '0.00', '0']
['3', 'Martin Atkinson', '3', '11.00', '0.69', '0.67', '1.67', '5', '0.00', '0']
['4', 'Graham Scott', '3', '10.00', '0.67', '0.00', '0.33', '1', '0.00', '0']
['5', 'Chris Kavanagh', '3', '10.33', '0.74', '0.00', '1.67', '5', '0.00', '0']
['6', 'Michael Oliver', '2', '12.00', '0.62', '1.00', '2.00', '4', '0.00', '0']
['7', 'Paul Tierney', '2', '12.50', '0.66', '0.00', '2.50', '5', '0.00', '0']
['8', 'Mike Dean', '2', '14.50', '0.73', '0.50', '3.00', '6', '0.00', '0']
['9', 'Andre Marriner', '2', '8.50', '0.53', '0.00', '2.00', '4', '0.00', '0']
['10', 'Kevin Friend', '2', '9.50', '0.50', '0.00', '3.00', '6', '0.00', '0']
['11', 'Craig Pawson', '1', '9.00', '0.45', '0.00', '1.00', '1', '0.00', '0']
['12', 'Stuart Attwell', '1', '9.00', '0.75', '0.00', '2.00', '2', '0.00', '0']
['13', 'Lee Probert', '1', '11.00', '0.73', '0.00', '0.00', '0', '0.00', '0']
['', 'Total / Average', '32', '11.09', '0.66', '0.19', '1.81', '58', '0.03', '1']
I was helped a bit by this answer during my research, but it still only puts everything into one single column while I'd want a separate column for every element of a particular list. Is there some way to achieve this?
Upvotes: 2
Views: 87
Reputation: 13401
Use pandas.DataFrame.from_records
:
import pandas as pd
df = pd.DataFrame.from_records(x[1:]))
print(df)
Output:
0 1 2 3 4 5 6 7 8 9
0 1 Anthony Taylor 6 11.17 0.68 0.17 1.83 11 0.17 1
1 2 Jonathan Moss 4 12.75 0.73 0.00 2.00 8 0.00 0
2 3 Martin Atkinson 3 11.00 0.69 0.67 1.67 5 0.00 0
3 4 Graham Scott 3 10.00 0.67 0.00 0.33 1 0.00 0
4 5 Chris Kavanagh 3 10.33 0.74 0.00 1.67 5 0.00 0
5 6 Michael Oliver 2 12.00 0.62 1.00 2.00 4 0.00 0
6 7 Paul Tierney 2 12.50 0.66 0.00 2.50 5 0.00 0
7 8 Mike Dean 2 14.50 0.73 0.50 3.00 6 0.00 0
8 9 Andre Marriner 2 8.50 0.53 0.00 2.00 4 0.00 0
9 10 Kevin Friend 2 9.50 0.50 0.00 3.00 6 0.00 0
10 11 Craig Pawson 1 9.00 0.45 0.00 1.00 1 0.00 0
11 12 Stuart Attwell 1 9.00 0.75 0.00 2.00 2 0.00 0
12 13 Lee Probert 1 11.00 0.73 0.00 0.00 0 0.00 0
13 Total / Average 32 11.09 0.66 0.19 1.81 58 0.03 1
Upvotes: 2
Reputation: 862641
You can filter out first sublist and pass to DataFrame constructor, also is possible set columns names by another list:
L = [[], ['1', 'Anthony Taylor', '6', '11.17', '0.68', '0.17', '1.83', '11', '0.17', '1'], ['2', 'Jonathan Moss', '4', '12.75', '0.73', '0.00', '2.00', '8', '0.00', '0'], ['3', 'Martin Atkinson', '3', '11.00', '0.69', '0.67', '1.67', '5', '0.00', '0'], ['4', 'Graham Scott', '3', '10.00', '0.67', '0.00', '0.33', '1', '0.00', '0'], ['5', 'Chris Kavanagh', '3', '10.33', '0.74', '0.00', '1.67', '5', '0.00', '0'], ['6', 'Michael Oliver', '2', '12.00', '0.62', '1.00', '2.00', '4', '0.00', '0'], ['7', 'Paul Tierney', '2', '12.50', '0.66', '0.00', '2.50', '5', '0.00', '0'], ['8', 'Mike Dean', '2', '14.50', '0.73', '0.50', '3.00', '6', '0.00', '0'], ['9', 'Andre Marriner', '2', '8.50', '0.53', '0.00', '2.00', '4', '0.00', '0'], ['10', 'Kevin Friend', '2', '9.50', '0.50', '0.00', '3.00', '6', '0.00', '0'], ['11', 'Craig Pawson', '1', '9.00', '0.45', '0.00', '1.00', '1', '0.00', '0'], ['12', 'Stuart Attwell', '1', '9.00', '0.75', '0.00', '2.00', '2', '0.00', '0'], ['13', 'Lee Probert', '1', '11.00', '0.73', '0.00', '0.00', '0', '0.00', '0'], ['', 'Total / Average', '32', '11.09', '0.66', '0.19', '1.81', '58', '0.03', '1']]
cols = ['id','name','a','b','c','d','e','f','g','h']
df = pd.DataFrame(L[1:], columns=cols)
print(df)
id name a b c d e f g h
0 1 Anthony Taylor 6 11.17 0.68 0.17 1.83 11 0.17 1
1 2 Jonathan Moss 4 12.75 0.73 0.00 2.00 8 0.00 0
2 3 Martin Atkinson 3 11.00 0.69 0.67 1.67 5 0.00 0
3 4 Graham Scott 3 10.00 0.67 0.00 0.33 1 0.00 0
4 5 Chris Kavanagh 3 10.33 0.74 0.00 1.67 5 0.00 0
5 6 Michael Oliver 2 12.00 0.62 1.00 2.00 4 0.00 0
6 7 Paul Tierney 2 12.50 0.66 0.00 2.50 5 0.00 0
7 8 Mike Dean 2 14.50 0.73 0.50 3.00 6 0.00 0
8 9 Andre Marriner 2 8.50 0.53 0.00 2.00 4 0.00 0
9 10 Kevin Friend 2 9.50 0.50 0.00 3.00 6 0.00 0
10 11 Craig Pawson 1 9.00 0.45 0.00 1.00 1 0.00 0
11 12 Stuart Attwell 1 9.00 0.75 0.00 2.00 2 0.00 0
12 13 Lee Probert 1 11.00 0.73 0.00 0.00 0 0.00 0
13 Total / Average 32 11.09 0.66 0.19 1.81 58 0.03 1
Upvotes: 3