I'll explain step-by-step after giving you a solution.
Here's a one-liner that will work.
df[df[0]=='#'] = df[df[0]=='#'].fillna(5)
To make the solution more general, I used the column's index based on your screenshot. You can change the index number, or specify by name like so:
df['name_of_column']
Step-by-step explanation:
First, you want to use the variable attributes in your first column df[0]
to select only those equal to string '#'
:
df[df[0]=='#']
Next, use the pandas fillna
method to replace all variable attributes that are np.NaN
with 5
:
df[df[0]=='#'].fillna(5)
According to the fillna
documentation, this function returns a new dataframe. So, to avoid this, you want to set the subsection of your dataframe to what is returned by the function:
df[df[0]=='#'] = df[df[0]=='#'].fillna(5)