TransWikia.com

Efficiently Zip multiple list in python/pandas

Stack Overflow Asked by Sunni on December 14, 2020

I have below Lists

Number = [[1],[2],[3],[4],[6]]
L1 = ['A','B','C','D','E']
L2 = [100, 55, 315, 68, 23]
L3 = ['18%','105','56%','12%','4%']

I wanted to Zip all the lists and create a DataFrame. I used the below code and successfully able to do it.

for n, l1, l2, l3 in zip(Number,L1,L2, L3):
    n.insert(1,l1)
    n.insert(2,l2)
    n.insert(3,l3)

df = pd.DataFrame(Number, columns=['Number','Name', 'Value', 'Score'])
print(df)

+---+--------+------+-------+-------+
|   | Number | Name | Value | Score |
+---+--------+------+-------+-------+
| 0 |    1   |   A  |  100  |  18%  |
+---+--------+------+-------+-------+
| 1 |    2   |   B  |   55  |  105  |
+---+--------+------+-------+-------+
| 2 |    3   |   C  |  315  |  56%  |
+---+--------+------+-------+-------+
| 3 |    4   |   D  |   68  |  12%  |
+---+--------+------+-------+-------+
| 4 |    6   |   E  |   23  |   4%  |
+---+--------+------+-------+-------+

Since there is only 4 lists in this example. Easily we can type manually for n, l1, l2, l3 in zip(Number,L1,L2, L3): and type individual insert functions.
Now my question is, what if there is many lists (say 15)? is there a pythonic way of doing this?

3 Answers

I am not sure why you have Number be a list of lists. But, this may be what you're looking for:

Number = [1,2,3,4,6]
L1 = ['A','B','C','D','E']
L2 = [100, 55, 315, 68, 23]
L3 = ['18%','105','56%','12%','4%']

pd.DataFrame(list(zip(Number, L1, L2, L3)), columns=['Number', 'Name', 'Value', 'Score'])

output:

   Number Name  Value Score
0       1    A    100   18%
1       2    B     55   105
2       3    C    315   56%
3       4    D     68   12%
4       6    E     23    4%

Answered by AlexanderHughes on December 14, 2020

I would definitely find a more resumed format to write this query! Out of the top of my head, I thought about this:

test = [Number, L1, L2, L3]
for t in zip(*test):
    print(t)

# on the first loop iteration:
# t = ([1], 'A', 100, '18%')
#t[3] = ['18%']

As you'll find, your results will now be in a tuple (e.g. ([1], 'A', 100, '18%')). So, if you need to access each value, e.g. the value from Number, you can do t[0].

Answered by rmssoares on December 14, 2020

Does this meet the requirement? Make a dictionary with the name of each list, and the list itself. Then pass the dictionary to the DataFrame constructor (in the print statement below).

data = {
    'Number': Number,
    'L1': L1,
    'L2': L2,
    'L3': L3
}
df = pd.DataFrame(data).rename(columns={'L1': 'Name', 
                                        'L2': 'Value', 
                                        'L3': 'Score'})
print(df)

   Number Name  Value Score
0       1    A    100   18%
1       2    B     55   105
2       3    C    315   56%
3       4    D     68   12%
4       6    E     23    4%

Answered by jsmart on December 14, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP