![](/img/trans.png)
[英]Pandas - replace values of a column if other values from same row appear in second data frame
[英]Pandas - Replace values in column with other values from the same column
具有3列的數據框 :
FLAG CLASS CATEGORY
yes 'Sci' 'Alpha'
yes 'Sci' 'undefined'
yes 'math' 'Beta'
yes 'math' 'undefined'
yes 'eng' 'Gamma'
yes 'math' 'Beta'
yes 'eng' 'Gamma'
yes 'eng' 'Omega'
yes 'eng' 'Omega'
yes 'eng' 'undefined'
yes 'Geog' 'Lambda'
yes 'Art' 'undefined'
yes 'Art' 'undefined'
yes 'Art' 'undefined'
我想用該類具有的其他類別值(如果有的話)填充CATEGORY列中的“未定義”值。 例如,Science類將用“ Alpha”填充其空白類別,“ math”類將用“ Beta”填充其“未定義”類別。
如果要考慮2個或更多類別,請保持原樣。 例如,英語課“ eng”有兩個類別“ Gamma”和“ Omega”,因此英語課的“ undefined”類別將保留為“ undefined”
如果某個類的所有類別均為“未定義”,則保留為“未定義”。
結果
FLAG CLASS CATEGORY
yes 'Sci' 'Alpha'
yes 'Sci' 'Alpha'
yes 'math' 'Beta'
yes 'math' 'Beta'
yes 'eng' 'Gamma'
yes 'math' 'Beta'
yes 'eng' 'Gamma'
yes 'eng' 'Gamma'
yes 'eng' 'Omega'
yes 'eng' 'Omega'
yes 'eng' 'undefined'
yes 'Geog' 'Lambda'
yes 'Art' 'undefined'
yes 'Art' 'undefined'
yes 'Art' 'undefined'
需要通用化。 我在班級欄目中有很多班級,不能負擔編碼“ Sci”或“ eng”的編碼。
我一直在嘗試使用多個np.wheres,但是沒有運氣。
我將在groupby
使用ffill
和bffil
s=df.CATEGORY.mask(df.CATEGORY.eq('undefined'))
s2=s.groupby(df['CLASS']).transform('nunique')
df.loc[s2.eq(1)&s.isnull(),'CATEGORY']=s.groupby(df.CLASS).apply(lambda x : x.ffill().bfill())
df
Out[388]:
FLAG CLASS CATEGORY
0 yes Sci Alpha
1 yes Sci Alpha
2 yes math Beta
3 yes math Beta
4 yes eng Gamma
5 yes math Beta
6 yes eng Gamma
7 yes eng Omega
8 yes eng Omega
9 yes eng undefined
10 yes Geog Lambda
11 yes Art undefined
12 yes Art undefined
13 yes Art undefined
請嘗試以下方法:
df['CATEGORY'] = df.replace('undefined', np.nan, regex=True).groupby('CLASS')['CATEGORY'].apply(lambda x: x.fillna(x.mode()[0]) if not x.isna().all() else x).replace(np.nan, "\\'undefined\\'")
編輯 :
我添加了另一個使用isin
解決方案來過濾掉有效class
以更新not undefined
和undefined
。 然后,更新df
確切切片。
步驟 :
將m
創建為CLASS
系列將CATEGORY
為undifined
且唯一not undefined
值。 使用isin
選擇限定行並where
把undefined
到NaN
。 最后,在這些行上按CLASS
進行Groupby
, ffill
, bfill
填充NaN
並分配回df
m = df.query('CATEGORY!="undefined"').drop_duplicates().CLASS.drop_duplicates(keep=False)
df[df.CLASS.isin(m)] = df[df.CLASS.isin(m)].where(df!='undefined').groupby('CLASS').ffill().bfill()
該解決方案看起來更干凈,但是我不知道它是否比使用groupby
慢
原件 :
我的解決方案根據唯一的'not undefined'
值映射的'undefined'
構造'not undefined'
:
m = df.query('CATEGORY != "undefined"').drop_duplicates().CLASS.drop_duplicates(keep=False)
t = df.query('CATEGORY == "undefined"').CLASS.map(df.loc[m.index].set_index('CLASS').CATEGORY)
df['CATEGORY'].update(t)
Out[553]:
FLAG CLASS CATEGORY
0 yes Sci Alpha
1 yes Sci Alpha
2 yes math Beta
3 yes math Beta
4 yes eng Gamma
5 yes math Beta
6 yes eng Gamma
7 yes eng Omega
8 yes eng Omega
9 yes eng undefined
10 yes Geog Lambda
11 yes Art undefined
12 yes Art undefined
13 yes Art undefined
您可以使用布爾型indesing
df[(df['CLASS']=='Sci'& df['CATEGORY']=='undefined','CATEGORY')]='Alpha'
df[(df['CLASS']=='math'& df['CATEGORY']=='undefined','CATEGORY')]='Beta'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.