1

I have a DataFrame with column3 containing NaN values. I want to replace these NaN values with column2-column1. But column 2 is a string and I want to take the first four digits from the string and convert it to integer before subracting.

I tried this:

df.column3.fillna(int(df.column2[:4]) - df.column1) 

And I get this following error:

TypeError: cannot convert the series to <class 'int'>

2 Answers 2

2

Use str[:4] for first 4 values of strings with convert to numeric by Series.astype:

df.column3 = df.column3.fillna(df.column2.str[:4].astype(int) - df.column1) 

Or by to_numeric if first solution failed:

df.column3 = df.column3.fillna(pd.to_numeric(df.column2.str[:4], errors='coerce') - df.column1) 
Sign up to request clarification or add additional context in comments.

Comments

1

Use:

df.column3.fillna(df.column2.str[:4].astype('int') - df.column1) 

1 Comment

Worked perfectly!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.