'Fill Nan in one specific row with the first value in dataframe Python
I want to fill that specific row with the same value "ZARATE".
#list of the indexes of rows containing Nan
rows_with_nan = [index for index, row in df.iterrows() if row.isnull().any()]
print(rows_with_nan)
#output 34 just one row in this case
if not(bool(rows_with_nan)):
#if list not empty
for l in rows_with_nan:
row= df.loc[l]
df= df.fillna(row['berth'].iloc[0]) # the problem is here if i put fillna it will change all the missing rows
Solution 1:[1]
Possible solution is the following:
import pandas as pd
# set test data and create dataframe
data = {"col1": ["one", "two", "three", "four", "five"], "col2": [None, 2, 3, None, 5], "col3": [None, 2, 3, None, 5]}
df= pd.DataFrame(data)
If you need to replace all NaN in rows with first column value
df.apply(lambda x : x.fillna(value=df.iloc[:,0]))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | gremur |


