1

I am working with this data frame depicted in the picture: enter image description here

I wanted to fill in nan values in the Date column and wrote the following code:

df['Date']=df['Date'].apply(lambda d: '1' if d==np.nan else d)

but it does not work for me. I totally got confused.

4
  • What's the datatype of that column? np.nan only works if it is a floating-point column. Pandas has an isna function for checking for NaNs. Commented Jul 20, 2022 at 17:44
  • Use is to compare np.nans: np.nan == np.nan <- this is False np.nan is np.nan <- this is True. Commented Jul 20, 2022 at 17:47
  • 1
    Don't compare np.nan with ==. Use pd.isnull(d) instead. Commented Jul 20, 2022 at 17:48
  • Note that it is part of the IEEE standard that nan != nan. Commented Jul 20, 2022 at 17:53

1 Answer 1

1

try doing it in 1 of these 3 ways

for all nan

df.fillna('1', inplace=True)

for specific col

df["Date"] = df["Date"].fillna('1')

df.Date = df.Date.fillna('1')

with lambda function

df['Date']=df['Date'].apply(lambda d: '1' if pd.isnull(d) else d)

to set the value based on month or day columns requires access to those columns, as well as the row the current null is at. This means you probably don't want to apply the changes in a .apply on the "date" col specifically

Instead, get the na mask for that col, and apply the change to the "date" col based on that

import pandas as pd

df = {
    "Date": ["2020-02-12", None, "2020-04-12"],
    "Month": ["Feb", "March", "April"]    
}
df = pd.DataFrame(df)
print('df before set: \n',df)
# get na 
date_is_null = (df["Date"].isna())

# apply dynamic value to null depending on value in another column
df.loc[date_is_null, 'Date']=df.loc[date_is_null, "Month"]
print('df after set: \n',df)

output

df before set: 
          Date  Month
0  2020-02-12    Feb
1        None  March
2  2020-04-12  April
df after set: 
          Date  Month
0  2020-02-12    Feb
1       March  March
2  2020-04-12  April
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry for the confusing question. I need to fill the null values with specific values for each of them. Thus, I decided to use lambda function.
@SikSik what do you mean by "specific values for each of them". is each subsequent na date supposed to increase the number it's set to? your lambda makes the intent look like you want to set all na's in the Date col to "1". either of the last 2 options will do that
I just wanted to show my way of using lambda function. In fact I will fill the null values of the Date column by making use of values of Month and Day_Name.
@SikSik update my answer to include a way to dynamically set the replacement for null's based on the value in a different column on the null row
Thanks, it was a wise answer. Moreover, I can do arithmetic operations among columns and this makes your answer very fit to my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.