首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从乘法表打印输出生成DataFrame -Python3.x

从乘法表打印输出生成DataFrame -Python3.x
EN

Stack Overflow用户
提问于 2018-07-09 04:29:45
回答 2查看 373关注 0票数 0

在下面的代码中,我生成了随机生成的乘法表的打印输出。我想让每个表生成一个DataFrame。我该怎么做呢?( Python 3.x中的新功能)

这个练习是为了生成一个乘法表。它扩展到一个项目,用随机生成的一位或两位数的列数和行数生成一组乘法表。目前,它设置为运行5个表,每个表有8列和8行。但是,这些数字是可以更改的。Jupyter Notebook最多只能打印12列。虽然我们的程序将生成任意多的列和行(大小相同,例如6x6、3x3、9x9等),但最好将其限制为12x12或更小的矩阵。

代码语言:javascript
复制
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')

谢谢。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51235756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档