在下面的代码中,我生成了随机生成的乘法表的打印输出。我想让每个表生成一个DataFrame。我该怎么做呢?( Python 3.x中的新功能)
这个练习是为了生成一个乘法表。它扩展到一个项目,用随机生成的一位或两位数的列数和行数生成一组乘法表。目前,它设置为运行5个表,每个表有8列和8行。但是,这些数字是可以更改的。Jupyter Notebook最多只能打印12列。虽然我们的程序将生成任意多的列和行(大小相同,例如6x6、3x3、9x9等),但最好将其限制为12x12或更小的矩阵。
import pandas as pd
import numpy as np
%matplotlib notebook
# This sets up how many tables we will generate
for t in range(0,5):
# Make variable place holders for our columns and rows list
a=[]
b=[]
# To use randomly generated numbers, this sets up the random column numbers 'a' and random row numbers 'b'
import random
for x in range(12):
a.append(random.randint(41,99)) # We can adjust the range of the random selection of numbers here
b.append(random.randint(1,35)) # We can adjust the range of the random selection of number here
# Add the column titles for each table - these are the random numbers 'a'
print("C/R: ", end="\t ")
for number in a:
print(number,end = '\t ')
print()
# The double for-loop to generate the table
for row in b:
print(row, end="\t") # First column
for number in a:
print(round(row*number,1),end='\t' )# Next columns
print( )
# Add two blank cosmetic lines between tables for readability
print('\n\n')
谢谢。
https://stackoverflow.com/questions/51235756
复制相似问题