2017-05-12 292 views
0
print(ScimEn.head(20)) 
    Rank    Country 
0  1    China 
1  2  United States 
2  3    Japan 
3  4  United Kingdom 
4  5 Russian Federation 
5  6    Canada 
6  7    Germany 
7  8    India 
8  9    France 
9  10   South Korea 
10 11    Italy 
11 12    Spain 
12 13    Iran 
13 14   Australia 
14 15    Brazil 
15 16    Taiwan 
16 17    Turkey 
17 18    Norway 
18 19   Netherlands 
19 20    Sweden 

我想合併這個DF到另一個,但只有排名[0:15]的行。 如果我做定期的:熊貓某些行數據幀合併

dfs = [ScimEn, energy, GDP[year].reset_index()] 
newdf = reduce(lambda left, right: pd.merge(left, right, on='Country'), dfs) 
newdf = newdf.set_index('Country') 
print(len(newdf)) 

我有140個條目的DF,但我想有按照上述排名提到15項的DF。 我也試過:

dfs = [ScimEn.where(ScimEn['Rank'] < 15), energy, GDP[year].reset_index()] 
newdf = reduce(lambda left, right: pd.merge(left, right, on='Country'), dfs) 
newdf = newdf.set_index('Country') 
print(len(newdf)) 

但它只返回4個條目。

+0

你能後你要合併到DFS的樣本? –

+0

您可以將ScimEn數據框分割爲先需要的然後正常合併。 ScimEn [ScimEn ['Rank'] <15]'會返回你需要的df。 –

回答

1

我想你可以通過query過濾:提到Phong Phung comment

dfs = [ScimEn.query("Rank < 15"), energy, GDP[year].reset_index()] 
newdf = reduce(lambda left, right: pd.merge(left, right, on='Country'), dfs) 
newdf = newdf.set_index('Country') 
print(len(newdf)) 

或者通過boolean indexing

dfs = [ScimEn[ScimEn['Rank'] < 15], energy, GDP[year].reset_index()] 
newdf = reduce(lambda left, right: pd.merge(left, right, on='Country'), dfs) 
newdf = newdf.set_index('Country') 
print(len(newdf))