2017-08-14 65 views
2

我有2個具有相同列的熊貓數據幀(df1和df2),我試圖從df1複製1行到df2的多行。 df2是一個多索引數據幀,第一個索引對應於df1的索引值,第二個索引是一個整數值。從熊貓數據幀複製一行到另一行的多行

這裏是他們是如何定義的:

​​

他們是什麼樣子:

In : df1 
Out: 
      c1  c2  c3  c4 
one 0.158366 0.843546 0.810493 0.925164 
two 0.880147 0.464835 0.416196 0.389786 
three 0.138132 0.061891 0.320366 0.727997 


In : df2 
Out: 
      c1 c2 c3 c4 
one 0 NaN NaN NaN NaN 
     1 NaN NaN NaN NaN 
two 0 NaN NaN NaN NaN 
     1 NaN NaN NaN NaN 
     2 NaN NaN NaN NaN 
three 0 NaN NaN NaN NaN 

現在,這裏是我已成功地從DF1中的數據複製到DF2:

for index, data in df1.iterrows(): 
    num = len(df2.loc[index]) 
    for i in range(num): 
     df2.loc[(index, i)] = df1.loc[index] 

結果:

In : df2 
Out: 
       c1   c2  c3  c4 
one 0 0.158366 0.843546 0.810493 0.925164 
     1 0.158366 0.843546 0.810493 0.925164 
two 0 0.880147 0.464835 0.416196 0.389786 
     1 0.880147 0.464835 0.416196 0.389786 
     2 0.880147 0.464835 0.416196 0.389786 
three 0 0.138132 0.0618906 0.320366 0.727997 

任何想法如何能更有效地做到這一點?

回答

2

您可以使用DataFrame.align,在什麼回報的元組DataFrames,所以選擇第二個加[1]

np.random.seed(23) 
df1 = pd.DataFrame(index=['one', 'two', 'three'], columns=['c1', 'c2', 'c3', 'c4'], data=np.random.random((3, 4))) 

index = pd.MultiIndex.from_arrays([['one', 'one', 'two', 'two', 'two', 'three'], [0, 1, 0, 1, 2, 0]]) 
df2 = pd.DataFrame(index=index, columns=['c1', 'c2', 'c3', 'c4']) 

print (df1) 
      c1  c2  c3  c4 
one 0.517298 0.946963 0.765460 0.282396 
two 0.221045 0.686222 0.167139 0.392442 
three 0.618052 0.411930 0.002465 0.884032 


df3 = df2.align(df1, level=0)[1] 
print (df3) 
       c1  c2  c3  c4 
one 0 0.517298 0.946963 0.765460 0.282396 
     1 0.517298 0.946963 0.765460 0.282396 
two 0 0.221045 0.686222 0.167139 0.392442 
     1 0.221045 0.686222 0.167139 0.392442 
     2 0.221045 0.686222 0.167139 0.392442 
three 0 0.618052 0.411930 0.002465 0.884032 
+1

我從來沒有見過'.align僞()'方法之前 - 謝謝您! – MaxU

+1

完美的作品!謝謝 – Arthur