Sinchetru Sinchetru - 1 month ago 13
Python Question

Dropping values only from a column pandas

I have a DataFrame with some string values that I am transforming to integer, for future calculations. The values from column

df['Age']
have some errors that are marked as
NaN
values. Using
dropna
function I lose a half of entire rows and a lot of data that is in others columns. How can I select for further calculations only the values from
df['Age']
that are not
NaN
values without changing the main DataFrame.

df['Age'] = np.where(pd.to_numeric(df['Age'], 'coerce').notnull(), df['Age'], NaN)
df = df.dropna(subset=['Age'])
df['Age'] = df['Age'].astype(int)

Answer

To select the rows with age not null:

df[df.Age.notnull()]

Or perhaps you want the age only:

df[[df.Age.notnull()].Age
Comments