1

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
    2             1       1       NaN
    3   FF       Sha      NaN     PA
    3             1        0       1

I need to replace NAN value in col_1, col_2, col_3 with "F" when NAN is not repeated in two Consecutive rows. The output is like this:

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

This is my code:
for col in ['col_1', 'col_2', 'col_3']:
    data = np.reshape(df[col].values, (-1, 2))
    need_fill = np.logical_and(data[:, 0] == '', data[:, 1] != '')
    data[np.where(need_fill),1] = 'F'

But it replace the 0 under NAN value to F. How I can fix the code to replace NAN to F.

Mary
  • 1,142
  • 1
  • 16
  • 37

1 Answers1

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")
niraj
  • 17,498
  • 4
  • 33
  • 48
  • thank you very much. When I applied your solution to my file there is no output and even no error. Is it possible to upload the sort of data and you can test it ? – Mary Jun 25 '17 at 17:59
  • @Mary updated with content for `.csv` file. You can use `read_csv` to get input `dataframe`. – niraj Jun 25 '17 at 18:12