Reputation: 73
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'>
Upvotes: 1
Views: 80
Reputation: 34056
Use:
df.column3.fillna(df.column2.str[:4].astype('int') - df.column1)
Upvotes: 1
Reputation: 862681
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)
Upvotes: 2