Python 从引用矩阵和值列表创建数据帧

Python 从引用矩阵和值列表创建数据帧,python,pandas,Python,Pandas,我有一个数据帧: import numpy as np import pandas as pd df = pd.DataFrame(np.random.randint(1,3,size=(4,3))) Out[0] : 0 1 2 0 2 2 1 1 2 2 2 2 1 1 1 3 2 1 2 以及一个值列表: L = np.random.random_integers(10,15,size=df.values.sum())

我有一个数据帧:

import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randint(1,3,size=(4,3)))

Out[0] :
    0   1   2
0   2   2   1
1   2   2   2
2   1   1   1
3   2   1   2
以及一个值列表:

L = np.random.random_integers(10,15,size=df.values.sum())

Out[1] : 
array([13, 11, 15, 11, 15, 13, 12, 11, 12, 15, 11, 11, 10, 11, 13, 11, 14,
       10, 12])
我需要您的帮助来创建一个与df大小相同的新数据框,该数据框的值为列表L,给出了引用矩阵df:

    0           1            2
0   [13, 11]    [15, 11]    [15]
1   [13, 12]    [11, 12]    [15, 11]
2   [11]        [10]        [11]
3   [13, 11]    [14]        [10, 12]

简单嵌套循环变量:

import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randint(1,3,size=(4,3)))

L = np.random.random_integers(10,15,size=df.values.sum())

new_df = df.astype(object).copy()
L_ind = 0
for i in range(df.shape[0]):
    for j in range(df.shape[1]):
        new_df.loc[i, j] =  list(L[L_ind: L_ind + df.iloc[i, j]])
        L_ind += df.iloc[i, j]
df:

L:

新的"基本法":

          0         1         2
0  [15, 12]  [10, 12]      [13]
1      [15]      [13]  [13, 15]
2      [13]  [15, 15]  [12, 11]
3  [14, 11]  [10, 15]  [15, 13]
这段代码可能会有所帮助

import numpy as np
import pandas as pd

np.random.seed(7)
df = pd.DataFrame(np.random.randint(1,3,size=(4,3)))
# print df

L = np.random.random_integers(10,15,size=df.values.sum())
currentIndex=0
new_df = pd.DataFrame()
for c in df.columns.tolist():
    new_list = []
    for val in df[c]:
        small_list = []
        for i in range(val):
            small_list.append(L[currentIndex])
            currentIndex+=1
        new_list.append(small_list)
    new_df.insert(c,c,new_list)

print new_df  
新德里

0         1         2
0  [10, 11]      [14]  [14, 15]
1      [12]  [10, 13]  [10, 10]
2  [12, 10]  [12, 13]      [15]
3  [14, 10]      [14]  [10, 13]

让我们看看你试过什么好吗?非常感谢,你的解决方案很有效。是否有使用df.applymap或一些类似函数的较短解决方案?
import numpy as np
import pandas as pd

np.random.seed(7)
df = pd.DataFrame(np.random.randint(1,3,size=(4,3)))
# print df

L = np.random.random_integers(10,15,size=df.values.sum())
currentIndex=0
new_df = pd.DataFrame()
for c in df.columns.tolist():
    new_list = []
    for val in df[c]:
        small_list = []
        for i in range(val):
            small_list.append(L[currentIndex])
            currentIndex+=1
        new_list.append(small_list)
    new_df.insert(c,c,new_list)

print new_df  
0         1         2
0  [10, 11]      [14]  [14, 15]
1      [12]  [10, 13]  [10, 10]
2  [12, 10]  [12, 13]      [15]
3  [14, 10]      [14]  [10, 13]