Skip to content Skip to sidebar Skip to footer

Replacing NaN Value With A Word When NaN Is Not Repeated In Two Consecutive Rows

for the following data frame: index Sent col_1 col_2 col_3 1 AB NaN DD CC 1 0 1 0 2 SA FA FB NaN

Solution 1:

May be there is something better, but one way would be to try using shift to see a row above and a row below. However, for first and last row, it would create issue. So, if it is not a problem to add extra rows and remove it later, you can try following:

# Appending row to the top: https://stackoverflow.com/a/24284680/5916727
df.loc[-1] = [0 for n in range(len(df.columns))]
df.index = df.index + 1  # shifting index
df = df.sort_index()  # sorting by index

# Append row to below it
df.loc[df.shape[0]] = [0 for n in range(len(df.columns))]
print(df)

   index Sent col_1 col_2 col_3
0      0    0     0     0     0
1      1   AB   NaN    DD    CC
2      1          0     1     0
3      2   SA    FA    FB   NaN
4      2          1     1   NaN
5      3   FF   Sha   NaN    PA
6      3          1     0     1
7      0    0     0     0     0

Now, check for consecutive rows using shift with masking by shift(-1) and shift(1):

columns = ["col_1", "col_2","col_3"]
for column in columns:
    df.loc[df[column].isnull() & df[column].shift(-1).notnull() &  df[column].shift(1).notnull(), column] = "F"
df = df [1:-1] # remove extra rows
print(df)

Output:

   index Sent col_1 col_2 col_3
1      1   AB     F    DD    CC
2      1          0     1     0
3      2   SA    FA    FB   NaN
4      2          1     1   NaN
5      3   FF   Sha     F    PA
6      3          1     0     1

If you want you can remove extra index column as well which seems to have duplicates.

Update (adding .csv data tested with)

I had following in the test csv file.

index,Sent,col_1,col_2,col_3
1,AB,,DD,CC
1, ,0,1,0
2,SA,FA,FB,NA
2, ,1,1,NA
3,FF,Sha,,PA
3, ,1,0,1

Then, you can use following to create input dataframe:

import pandas as pd
df = pd.read_csv("FILENAME.csv")

Post a Comment for "Replacing NaN Value With A Word When NaN Is Not Repeated In Two Consecutive Rows"