Skip to main content
 首页 » 编程设计

python之将列表转换为表之python

2025年05月04日20softidea

我有两个数组。 column_names 包含列标题。 values 包含所有值。

我明白如果我这样做:

column_names = ["a", "b", "c"] 
values = [1, 2, 3] 
for n, v in zip(column_names, values): 
    print("{} = {}".format(n, v)) 

我明白了

a = 1 
b = 2 
c = 3 

如果我通过了,我该如何编码:

column_names = ["a", "b", "c"] 
values = [1, 2, 3, 4, 5, 6, 7, 8, 9] 

我会得到

a = 1, 4, 7 
b = 2, 5, 8 
c = 3, 6, 9 

谢谢!

请您参考如下方法:

使用 pandas 和 numpy 很容易,结果将是一个更有用的表格。 Pandas 擅长排列表格数据。因此,让我们利用它: 安装 Pandas :

pip install pandas --user 
#pandas comes with numpy 
import numpy as np 
import pandas as pd 
 
# this makes a normal python list for integers 1-9 
input = list(range(1,10)) 
 
#lets convert that to numpy array as np.array 
num = np.array(input) 
 
#currently its shape is single dimensional, lets change that to a two dimensional matrix that turns it into the clean breaks you want 
reshaped = num.reshape(3,3) 
 
#now construct a beautiful table 
pd.DataFrame(reshaped, columns=['a','b','c']) 
 
#ouput is 
   a  b  c 
0  1  2  3 
1  4  5  6 
2  7  8  9