Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将两个列表相乘以生成python数据帧的智能方法_Python_List_Dataframe - Fatal编程技术网

将两个列表相乘以生成python数据帧的智能方法

将两个列表相乘以生成python数据帧的智能方法,python,list,dataframe,Python,List,Dataframe,我有两个数据列表,即: a = [1,1,1, 0,0,0] b = [9,8,7, 6,5,4] 我想要的结果是: df = [[9,9,9, 0,0,0], [8,8,8, 0,0,0], [7,7,7, 0,0,0], [6,6,6, 0,0,0], [5,5,5, 0,0,0], [4,4,4, 0,0,0]] 我目前正在做的是: for aa in a: counter = 0 df =

我有两个数据列表,即:

 a = [1,1,1, 0,0,0]
 b = [9,8,7, 6,5,4]
我想要的结果是:

df = [[9,9,9, 0,0,0],
       [8,8,8, 0,0,0],
       [7,7,7, 0,0,0],
       [6,6,6, 0,0,0],
       [5,5,5, 0,0,0],
       [4,4,4, 0,0,0]]
我目前正在做的是:

for aa in a:

    counter = 0

    df = pd.DataFrame()

    while counter<len(b): 

        df[counter] = pd.Series(b)*a[counter]

        counter+=1
对于a中的aa:
计数器=0
df=pd.DataFrame()

计数器时,您可以使用外部产品
np.outer
并使用结果初始化数据帧:

import numpy as np
import pandas as pd

a = [1,1,1, 0,0,0]
b = [9,8,7, 6,5,4]

pd.DataFrame(np.outer(b, a))

   0  1  2  3  4  5
0  9  9  9  0  0  0
1  8  8  8  0  0  0
2  7  7  7  0  0  0
3  6  6  6  0  0  0
4  5  5  5  0  0  0
5  4  4  4  0  0  0

你试过使用
a
b
的倒数的
numpy.matmul
吗?a和b的原始大小是否与你提供的示例相似?@bunbun我还没有试过,我会试一试too@Marmik沙阿,事实上,这会很有趣different@H.Choi啊!!提供的答案是好的。但通常,当您想要构建一个矩阵时,在大多数情况下,复杂性都是O(行*列)。这是因为您正在创建行*列值。哇!非常感谢你!优雅而高效的回答:)