BS98
BS98

Reputation: 101

How to fill missing values with conditions?

I have a pandas DataFrame like this:

year = [2015, 2016, 2009, 2000, 1998, 2017, 1980, 2016, 2015, 2015]
mode = ["automatic", "automatic", "manual", "manual", np.nan,'automatic', np.nan, 'automatic', np.nan, np.nan]

X = pd.DataFrame({'year': year, 'mode': mode})

print(X)

   year       mode
0  2015  automatic
1  2016  automatic
2  2009     manual
3  2000     manual
4  1998        NaN
5  2017  automatic
6  1980        NaN
7  2016  automatic
8  2015        NaN
9  2015        NaN

I want to fill missing values with like this: if year is <2010 I want to fill NaN with 'manual' and if year is >=2010 I want to fill NaN value with 'automatic'

I thought about combination .groupby function with these condition but I do not know honestly how to do it :(

I would be grateful for any help.

Upvotes: 2

Views: 3270

Answers (3)

Tom
Tom

Reputation: 8790

Similar approach to my answer on your other question:

cond = X['year'] < 2010
X['mode'] = X['mode'].fillna(cond.map({True:'manual', False: 'automatic'}))

Upvotes: 3

Ric S
Ric S

Reputation: 9247

You can use np.where

X['mode'] = X['mode'].fillna(pd.Series(np.where(X['year'] >= 2010, 'automatic', 'manual')))

Output

   year       mode
0  2015  automatic
1  2016  automatic
2  2009     manual
3  2000     manual
4  1998     manual
5  2017  automatic
6  1980     manual
7  2016  automatic
8  2015  automatic
9  2015  automatic

Upvotes: 0

BENY
BENY

Reputation: 323226

With np.where and fillna

s=pd.Series(np.where(X.year<2010,'manual','automatic'),index=X.index)
X['mode'].fillna(s,inplace=True)
X
Out[192]: 
   year       mode
0  2015  automatic
1  2016  automatic
2  2009     manual
3  2000     manual
4  1998     manual
5  2017  automatic
6  1980     manual
7  2016  automatic
8  2015  automatic
9  2015  automatic

Upvotes: 1

Related Questions