转载自:
https://www.cnblogs.com/jdwfff/p/11990552.html
https://www.it1352.com/1689965.html
- 方法一:直接df复制
data_p = pd.DataFrame(np.arange(25).reshape(5,5))
print(data_p)
pd2=pd.DataFrame()
for i in range(len(data_p)):
a=data_p.loc[i]
d=pd.DataFrame(a).T
pd2=pd2.append([d]*24) #每行复制24倍
print(pd2)
- 方法2:利用np
import numpy as np
import pandas as pd
arr = np.array(
[[1,0,1,1,1,5],
[0,0,0,0,1,3],
[1,0,0,0,1,1],
[1,0,0,1,1,1],
[1,0,0,0,1,1],
[1,1,0,0,1,1]]
)
df = pd.DataFrame(
np.repeat(arr, arr[:,5], axis=0),
columns=['var1','var2','var3','var4','var5','cnt']
)
print(df)
# var1 var2 var3 var4 var5 cnt
# 0 1 0 1 1 1 5
# 1 1 0 1 1 1 5
# 2 1 0 1 1 1 5
# 3 1 0 1 1 1 5
# 4 1 0 1 1 1 5
# 5 0 0 0 0 1 3
# 6 0 0 0 0 1 3
# 7 0 0 0 0 1 3
# 8 1 0 0 0 1 1
# 9 1 0 0 1 1 1
# 10 1 0 0 0 1 1
# 11 1 1 0 0 1 1