方法一:
import pandas as pd
from pandas import DataFrame
d_df = DataFrame(['房产证已拿到','房产证办理中','房产证没拿到'],columns=['房产证状态'])
w_df = DataFrame(['已经是苏州户口','苏州户口办理中','开学前没有户口'],columns=['户口状态'])
new_df = DataFrame(columns=['房产证状态','户口状态'])
for w_index,w_row in w_df.iterrows():
for d_index,d_row in d_df.iterrows():
w_data = w_row['户口状态']
d_data = d_row['房产证状态']
row = DataFrame([dict(户口状态=w_data, 房产证状态=d_data), ])
new_df = new_df.append(row,ignore_index=True)
new_df=new_df[['房产证状态','户口状态']] #默认排序是按照字母升序(a,b,c,d...),此代码是指定排序方式
new_df.index = range(1,len(new_df) + 1) # 将index改成从1开始
print (new_df)
print (new_df.shape)
#output:
房产证状态 户口状态
1 房产证已拿到 已经是苏州户口
2 房产证办理中 已经是苏州户口
3 房产证没拿到 已经是苏州户口
4 房产证已拿到 苏州户口办理中
5 房产证办理中 苏州户口办理中
6 房产证没拿到 苏州户口办理中
7 房产证已拿到 开学前没有户口
8 房产证办理中 开学前没有户口
9 房产证没拿到 开学前没有户口
(9, 2)
方法二:
import numpy as np
date_list = ['2018-01-01','2018-01-02','2018-01-03']
date_count = len(date_list)
workers = np.arange(3)
df = DataFrame(columns=['w','date'])
for worker in workers:
cur_arr = np.array([worker] * date_count)
cur_arr.shape = (date_count,1) #转置
date_list = np.array(date_list)
date_list.shape = (date_count,1) #转置
join_arr = np.hstack((cur_arr,date_list)) #水平方向合并
df = df.append(DataFrame(join_arr,columns=['w','date']),ignore_index=True)
print (df)
print (df.shape)
#output: w date
0 0 2018-01-01
1 0 2018-01-02
2 0 2018-01-03
3 1 2018-01-01
4 1 2018-01-02
5 1 2018-01-03
6 2 2018-01-01
7 2 2018-01-02
8 2 2018-01-03
(9, 2)
方法三:import itertools
for x in itertools.product(['a','b'],['01','02','03']):
print (x)
#output:
('a', '01')
('a', '02')
('a', '03')
('b', '01')
('b', '02')
('b', '03')