[英]How to create a dataframe with multiple lists/arrays in python
我有很多由一維數據組成的列表。 像下面這樣:
list1 = [1,2,3,4...]
list2 = ['a','b','c'...]
現在,我必須創建如下所示的數據框:
df = [[1,'a'],[2,'b'],[3,'c']]
我需要這個數據框,以便我可以使用 pandas_profiling 分析每一列。 請建議。
我試過了
list1+list2
但它給出的數據如下:
list3=[1,2,3,4...'a','b'...]
也使用了 numpy hpstack,但不起作用
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')
你可以這樣做:
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)
輸出:
list1 list2
0 1 a
1 2 b
2 3 c
3 4 d
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.