-1

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?

11
  • 1
    Does this answer your question? Python: create a pandas data frame from a list Commented Jul 31, 2020 at 17:06
  • No. I'm asking about efficient way of Zipping multiple files. the above is different. Commented Jul 31, 2020 at 17:08
  • where is the mention of anything about zipping multiple files in your question? Commented Jul 31, 2020 at 17:09
  • Check the Subject and final para.. Commented Jul 31, 2020 at 17:10
  • you can try pd.DataFrame(zip(np.concatenate(Number),L1,L2,L3)) Commented Jul 31, 2020 at 17:11

3 Answers 3

0

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%
Sign up to request clarification or add additional context in comments.

1 Comment

this answer doesn't work. because, Number is a list of lists. first we have to flatten this list by [item for sublist in Number for item in sublist]. after doing this, we can use your code
0

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].

1 Comment

Number is a list of list
0

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%

2 Comments

Number is a list of list
It seems like you want to flatten the list then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.