How to use multiple lists of lists to append new rows to a dataframe?
I am trying to use multiple list of lists to add rows to a dataframe.
The error is as follows: IndexError: invalid index to scalar variable.
The code is below:
List_a = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]]
List_b = [[16,17,18],[19,20,21],[22,23,24],[25,26,27],[28,29,30]]
List_c = [[31,32,33],[34,35,36],[37,38,39],[40,41,42],[43,44,45]]
Some_List = ['0', '1', '2', '3', '4']
first_row = {'A': [0], 'B': [0], 'C': [0]}
All_Rows = pd.DataFrame(first_row)
#Optimized_Trades
for i in range(len(Some_List)):
for j in range(len(Some_List[i])):
df_temp = { 'A': List_a[i][j], 'B': List_b[i][j], 'C': List_c[i][j]}
All_Rows = All_Rows.append(df_temp, ignore_index = True)
All_Trades = All_Trades[1:]
display(All_Trades)
Ideally, the final output would be:
A B C
1 16 31
2 17 32
3 18 33
4 19 34
5 20 35
6 21 36
7 22 37
8 23 38
9 24 39
10 25 40
11 26 41
12 27 42
13 28 43
14 29 44
15 30 45
Solution 1:
You can use itertools.chain
to flatten each list, construct a dictionary with the flattened lists and cast it to a DataFrame:
from itertools import chain
A, B, C = [list(chain.from_iterable(lst)) for lst in [List_a, List_b, List_c]]
out = pd.DataFrame({'A': A, 'B': B, 'C': C})
Output:
A B C
0 1 16 31
1 2 17 32
2 3 18 33
3 4 19 34
4 5 20 35
5 6 21 36
6 7 22 37
7 8 23 38
8 9 24 39
9 10 25 40
10 11 26 41
11 12 27 42
12 13 28 43
13 14 29 44
14 15 30 45
Solution 2:
It seems you need range(3)
, because length of sublists is 3
:
for i in range(len(Some_List)):
for j in range(3):
df_temp = { 'A': List_a[i][j], 'B': List_b[i][j], 'C': List_c[i][j]}
All_Rows = All_Rows.append(df_temp, ignore_index = True)
Or you can loop by List_a
with enumarate
, so inner loop use actual length of sublists:
for i, vals in enumerate(List_a):
for j, vals1 in enumerate(vals):
df_temp = { 'A': List_a[i][j], 'B': List_b[i][j], 'C': List_c[i][j]}
All_Rows = All_Rows.append(df_temp, ignore_index = True)
If need only flatten lists:
L = [List_a, List_b, List_c]
df = pd.DataFrame([[z for y in x for z in y] for x in L], index = ['A','B','C']).T
print (df)
A B C
0 1 16 31
1 2 17 32
2 3 18 33
3 4 19 34
4 5 20 35
5 6 21 36
6 7 22 37
7 8 23 38
8 9 24 39
9 10 25 40
10 11 26 41
11 12 27 42
12 13 28 43
13 14 29 44
14 15 30 45
Solution 3:
EDIT: Based on your fixed expected output, try this -
- Store all the list of lists as a list
l
- Next, add them directly to a dataframe
df
which would be a dataframe with 5 columns and 3 rows. - Take transpose
- Use explode on each of the columns
- Rename the columns
List_a = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]]
List_b = [[16,17,18],[19,20,21],[22,23,24],[25,26,27],[28,29,30]]
List_c = [[31,32,33],[34,35,36],[37,38,39],[40,41,42],[43,44,45]]
l = [List_a, List_b, List_c]
df = pd.DataFrame(l).T
output = df.explode(list(df.columns))
output.columns = ['A','B','C']
print(output)
A B C
0 1 16 31
0 2 17 32
0 3 18 33
1 4 19 34
1 5 20 35
1 6 21 36
2 7 22 37
2 8 23 38
2 9 24 39
3 10 25 40
3 11 26 41
3 12 27 42
4 13 28 43
4 14 29 44
4 15 30 45