I have a DataFrame like this:
ColA ColB ColC ColD ColE
Male 1 0 ? 1
Female 1 1 ? 1
Male 0 0 0 1
Female 1 0 0 ?
Female 1 1 1 ?
It has a number of ?
throughout. I'd like to replace these with the mode (most common value of each row) - excluding the first column.
I've done something like this, but doesn't return what I want:
df.replace("?", np.nan, inplace = True)
df_new = df.apply(lambda row: row.fillna(row[1:].mode()), axis = 1)
For fillna we can only fillna
with column
with series, so we need T
, and for the mode
we can do outside the apply
df_new = df.T.fillna(df.iloc[:,1:].astype(float).mode(axis=1)[0]).T
df_new
ColA ColB ColC ColD ColE
0 Male 1 0 1.0 1
1 Female 1 1 1.0 1
2 Male 0 0 0 1
3 Female 1 0 0 0.0
4 Female 1 1 1 1.0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.