Home > Software engineering >  Why lambda function does not apply in pandas
Why lambda function does not apply in pandas

Time:07-21

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.

CodePudding user response:

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
  • Related