2017-08-28 71 views
1

假設你有兩個DataFrames合併兩個DataFrames具有相同的架構

foo = pd.DataFrame([[123,321],[1543,432]], columns=['id','location']) 
bar = pd.DataFrame([[123,421],[1543,436]], columns=['location','id']) 

要合併成一個大桌子。然而,作爲架構(列)是一樣的,所得到的表應加表示「類型」的新列:

id location type 
0 123 321  foo 
1 1543 432  foo 
0 421 123  bar 
1 436 1543  bar 

目前,我做的是

foo['type'] = ['foo'] * foo.shape[0] 
bar['type'] = ['bar'] * bar.shape[0] 
pd.concat([foo,bar]) 

有一些更聰明的方式來做到這一點,特別是避免前兩行?

回答

0

您可以添加參數keysconcat - 它創造MultiIndex

print (pd.concat([foo,bar], keys=('foo','bar'))) 
     id location 
foo 0 123  321 
    1 1543  432 
bar 0 421  123 
    1 436  1543 

併爲MultiIndex列使用:

df = pd.concat([foo,bar], keys=('foo','bar')) 
     .reset_index(drop=True, level=1) 
     .rename_axis('type') 
     .reset_index() 
print (df) 
    type id location 
0 foo 123  321 
1 foo 1543  432 
2 bar 421  123 
3 bar 436  1543 

assign另一種解決方案:

df = pd.concat([foo.assign(type='foo'),bar.assign(type='bar')]) 
print (df) 
    id location type 
0 123  321 foo 
1 1543  432 foo 
0 421  123 bar 
1 436  1543 bar 
+0

你爲什麼編輯解決方案?第一個工作,以及,恕我直言,更可讀一點。 – Dror

+0

當然,它是回滾;) – jezrael

1

德羅爾下面是你在做代碼和@jezrael方法的過程中的最終結果。

import pandas as pd 
foo = pd.DataFrame([[123,321],[1543,432]], columns=['id','location']) 
bar = pd.DataFrame([[123,421],[1543,436]], columns=['location','id']) 


def f(foo,bar): 
    foo['type'] = ['foo'] * foo.shape[0] 
    bar['type'] = ['bar'] * bar.shape[0] 
    x = pd.concat([foo,bar]) 
    return x 

%timeit xx = f(foo,bar) 


>>>1.14 ms ± 5.56 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 


def f2(foo,bar): 
    pd.concat([foo,bar], keys=('foo','bar')) 
    df = pd.concat([foo,bar], keys=('foo','bar')).reset_index(level=0).rename_axis(None).rename(columns={'level_0':'type'}) 
    return df 
%timeit yy = f2(foo,bar) 

>>>3.04 ms ± 18.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) 

看起來你的代碼更快。

+0

WOW。這是一個有趣的觀點。這是每桌僅有2行。感謝提高這一點。 – Dror

相關問題