【发布时间】:2021-12-19 11:01:44
【问题描述】:
我有两个如下列表:-
column1= [30, 40, 50, 60,90,20,30,20,30]
column2= ['bat', 'ball','tent']
我想使用两个列表创建一个数据框,这样 column2 的值必须同样重复才能创建一个数据框。我尝试实现它,但得到如下输出:-
import pandas as pd
import numpy as np
column1= [30, 40, 50, 60,90,20,30,20,30]
column2= ['bat', 'ball','tent']
table1 = np.tile(column1, len(column2))
table2 = np.repeat(column2, len(column1))
df = pd.DataFrame({"table1": table1, "table2": table2})
print(df)
收到的输出:-
table1 table2
0 30 bat
1 40 bat
2 50 bat
3 60 bat
4 90 bat
5 20 bat
6 30 bat
7 20 bat
8 30 bat
9 30 ball
10 40 ball
11 50 ball
12 60 ball
13 90 ball
14 20 ball
15 30 ball
16 20 ball
17 30 ball
18 30 tent
19 40 tent
20 50 tent
21 60 tent
22 90 tent
23 20 tent
24 30 tent
25 20 tent
26 30 tent
异常输出:-
table1 table2
0 30 bat
1 40 bat
2 50 bat
3 60 ball
4 90 ball
5 20 ball
6 30 tent
7 20 tent
8 30 tent
有没有更好的方法来获取异常输出?
【问题讨论】:
标签: python pandas list dataframe numpy