Reputation: 25
Very new to coding so please excuse my lack of knowledge
I currently have a dataframe that looks like this:
date A B C
2006-11-01 NaN 1 NaN
2016-11-02 NaN NaN 1
2016-11-03 1 NaN NaN
2016-11-04 NaN 1 NaN
2016-11-05 NaN 1 NaN
2016-11-06 NaN NaN NaN
2016-11-07 NaN 1 NaN
What I want to do, for example, is:
replace all NaN's in column A with 0 for all dates after 2016-11-03 and be able to do this same thing for each column but with different corresponding dates.
I have tried
for col in df:
if col == 'A' & 'date' > '2016-11-03':
value_1 = {'A':0}
df = df.fillna(value=value_1)
but I received this error TypeError: unsupported operand type(s) for &: 'str' and 'str'
I'm sure this has to do with my lack of knowledge but I'm not sure how to proceed.
EDIT: what I am looking for is something like this:
date A B C
2006-11-01 NaN 1 NaN
2016-11-02 NaN NaN 1
2016-11-03 1 NaN NaN
2016-11-04 0 1 NaN
2016-11-05 0 1 NaN
2016-11-06 0 NaN NaN
2016-11-07 0 1 NaN
Upvotes: 0
Views: 48
Reputation: 1
You can use fillna, replace empty rows with zero
if condition == 'A':
fillna(0, inplace = True)
Upvotes: 0
Reputation: 696
df.loc[(df.date > '2016-11-03') & (df['A'].isnull()),'A'] = 0;
Output:
A B C date
0 NaN 1.0 NaN 2006-11-01
1 NaN NaN 1.0 2016-11-02
2 1.0 NaN NaN 2016-11-03
3 0.0 1.0 NaN 2016-11-04
4 0.0 1.0 NaN 2016-11-05
5 0.0 NaN NaN 2016-11-06
6 0.0 1.0 NaN 2016-11-07
Upvotes: 0
Reputation: 57033
The condition consists of two parts: being after a certain date and being a NaN.
condition = (df['date'] > '2016-11-03') & df['A'].isnull()
Now, choose the rows that match the condition and make the respective items in the column A equal 0:
df.loc[condition, 'A'] = 0
Upvotes: 3