Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a dataframe with multiple lists/arrays in python

I have many lists which consists of 1d data. like below:

list1 = [1,2,3,4...]
list2 = ['a','b','c'...] 

Now, I have to create dataframe like below:

df = [[1,'a'],[2,'b'],[3,'c']]

I need this dataframe so that I can profile each column using pandas_profiling. Please suggest.

I have tried

list1+list2

but its giving data like below:

list3=[1,2,3,4...'a','b'...]

used numpy hpstack too, but not working

import pandas as pd
import pandas_profiling
import numpy as np

list3 = np.hstack([[list1],[list2]])

array([[1,2,3,4,'a','b','c'..]],dtype='<U5')
like image 960
MSNGH Avatar asked Jan 17 '26 07:01

MSNGH


1 Answers

You can do in this way:

import pandas as pd

list1 = [1,2,3,4]
list2 = ['a','b','c','d']
list3 = zip(list1, list2)
df = pd.DataFrame(list3, columns=('list1', 'list2'))
print (df)

Output:

   list1 list2
0      1     a
1      2     b
2      3     c
3      4     d
like image 109
Joe Avatar answered Jan 19 '26 19:01

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!