## 从乘法表打印输出生成DataFrame - Python 3.x.内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (1)
• 关注 (0)
• 查看 (171)

``````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( )

print('\n\n')
``````

### 1 个回答

``````import pandas as pd
import numpy as np
%matplotlib notebook

# This sets up how many tables we will generate

dataframes = []

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

data = []

for row in b:
temp = []
for number in a:
temp.append(round(row*number,1))

data.append(temp)

dataframe = pd.DataFrame(data=data, columns=a)
dataframe.index = b

dataframes.append(dataframe)

dataframes[0]
``````