2017-04-25 104 views
2

一直在嘗試這一段時間,但沒有得到任何地方。 請考慮下面的DF。通過值計數過濾數據幀

Id YearBuilt SalePrice Neighborhood 
    1  2003  208500  CollgCr 
    2  1976  181500  Veenker 
    3  2001  223500  CollgCr 
    4  1915  140000  Crawfor 
    5  2000  250000  NoRidge 
    6  1993  143000  Mitchel 
    7  2004  307000  Somerst 
    8  1973  200000  NWAmes 
    9  1931  129900  OldTown 
    10  1939  118000  BrkSide 
    11  1965  129500  Sawyer 
    12  2005  345000  NridgHt 
    13  1962  144000  Sawyer 
    14  2006  279500  CollgCr 
    15  1960  157000  NAmes 
    16  1929  132000  BrkSide 
    17  1970  149000  NAmes 

我想將數據分組到社區和鄰里是否的計數小於說10,它應該被放入一個組other。我看到了其他答案,但無法解釋它們。我曾嘗試

house_df['newColumn'] = house_df['Neighborhood'].mask(house_df['Neighborhood'].count < 50, 'other') 

也試過

house_df.groupby['Neighborhood'].filter(lambda x: x.count < 10) 

但這並不工作。也試圖groupby鄰里和應用過濾器,但沒有去。請幫忙。

這是我所期待實現

Id YearBuilt SalePrice Neighborhood newColumn 
1  2003  208500  CollgCr Collgcr 
2  1976  181500  Veenker other 
3  2001  223500  CollgCr CollgCr 
4  1915  140000  Crawfor other 
5  2000  250000  NoRidge NoRidge 
6  1993  143000  Mitchel Mitchel 
7  2004  307000  Somerst other 
8  1973  200000  NWAmes NWAmes 
+0

請顯示其他失敗的嘗試。 –

+0

'house_df.groupby ['Neighborhood']。filter(lambda x:x.count <10)' – Harj

+0

請修改您的問題。這是相關信息 –

回答

2

使用value_counts計數街區和maplambda產生適當的分組的例子。

vc = df.Neighborhood.value_counts() 

df = df.assign(
    newColumn=df.Neighborhood.map(
     lambda x: x if vc.at[x] > 1 else 'other' 
    ) 
) 
+0

這不是我要找的。如果鄰居的數量少於10,我想把'鄰居'放到'other'組中。換句話說,我想減少維數,並且使其他鄰居組的鄰居數小於10。 – Harj

+0

@Harj這就是我做了什麼。除了我用了少於2個,因爲你沒有提供足夠的數據來顯示它與10的樣子。如果這仍然不是你想要的,考慮編輯你的問題,並明確你想要什麼。 – piRSquared

+0

我編輯了這個問題。謝謝 – Harj