[英]How do I quickly create a numpy or pandas 2D array with both axis in a range and the values a product?
标题比我预期的要复杂得多,但我基本上是在寻找一种快速的方法来制作一个乘法表,从X和Y轴的abitraty整数开始。
我的输出与此类似,因为X是(5, 12, 1)
的范围,Y是(20, 25, 1)
5 6 7 8 9 10 11
20 100 120 140 160 180 200 220
21 105 126 147 168 189 210 231
22 110 132 154 176 198 220 242
23 115 138 161 184 207 230 253
24 120 144 168 192 216 240 264
我发现这个答案看起来很相似,但是对于索引来说。 它似乎没有我正在寻找的乘法。
row = np.arange(20,25)
col = np.arange(5,12)
df = pd.DataFrame(row[:,None]*col,index=row,columns=col)
样品运行 -
In [224]: row = np.arange(20,25)
In [225]: col = np.arange(5,12)
In [226]: pd.DataFrame(row[:,None]*col,index=row,columns=col)
Out[226]:
5 6 7 8 9 10 11
20 100 120 140 160 180 200 220
21 105 126 147 168 189 210 231
22 110 132 154 176 198 220 242
23 115 138 161 184 207 230 253
24 120 144 168 192 216 240 264
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.