Fill empty values from a row with the value of next column on the same row on csv file with pandas

Viewed 162

I have this type of DataFrame

name     surname       middle

Frank    Doe           NaN
John     Nan           Wood
Jack     Putt          Nan
Frank    Nan           Joyce

I want to move "middle" values on NaN same rows values on "surname" column. How can i do this? I tried to use the fillna method but got no results. Here is my code:

import os
from pandas.io.parsers import read_csv


for csvFilename in os.listdir('.'):
   if not csvFilename.endswith('.csv'):
      continue
data=read_csv(csvFilename)
filtered_data["surname"].fillna(filtered_data["middle"].mean(),inplace=True)
filtered_data.to_csv('output.csv' , index=False)
2 Answers
Related