2017-09-21 54 views
-1

的名单我有一个出现数据框:创建从事件矩阵的熊猫数据框和值

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具有相同大小的新DataFrame,其具有列表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] 
+1

怎么样你告诉我们,你试过吗? – Julien

回答

0

简单的嵌套循环变种:

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:

0 1 2 
0 2 2 1 
1 1 1 2 
2 1 2 2 
3 2 2 2 

L:

array([15, 12, 10, 12, 13, 15, 13, 13, 15, 13, 15, 15, 12, 11, 14, 11, 10, 
     15, 15, 13]) 

new_df:

  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] 
+0

非常感谢,您的解决方案运作良好。有没有使用df.applymap或类似功能的较短解决方案? –

0

这个代码可以帮助

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 

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]