Skip to main content
deleted 1 character in body
Source Link
Mit
  • 716
  • 7
  • 19

OK, there is a simple way of doing this using ffill as below, however, this will only work if dependants are always following their employer in rows order (whichwhich is the case in the data sample you've provided hence the below code works:

import pandas as pd

#read in your csv file
df = pd.read_csv('fileName.csv')


#loop over columns and replace nans with the most recent value available
for c in df.columns:
    df[c].fillna(method='ffill', inplace=True)

#write out your df back to csv
df.to_csv('newFile.csv', index=False)

OK, there is a simple way of doing this using ffill as below, however, this will only work if dependants are always following their employer in rows order (which is the case in the data sample you've provided hence the below code works:

import pandas as pd

#read in your csv file
df = pd.read_csv('fileName.csv')


#loop over columns and replace nans with the most recent value available
for c in df.columns:
    df[c].fillna(method='ffill', inplace=True)

#write out your df back to csv
df.to_csv('newFile.csv', index=False)

OK, there is a simple way of doing this using ffill as below, however, this will only work if dependants are always following their employer in rows order which is the case in the data sample you've provided hence the below code works:

import pandas as pd

#read in your csv file
df = pd.read_csv('fileName.csv')


#loop over columns and replace nans with the most recent value available
for c in df.columns:
    df[c].fillna(method='ffill', inplace=True)

#write out your df back to csv
df.to_csv('newFile.csv', index=False)
Source Link
Mit
  • 716
  • 7
  • 19

OK, there is a simple way of doing this using ffill as below, however, this will only work if dependants are always following their employer in rows order (which is the case in the data sample you've provided hence the below code works:

import pandas as pd

#read in your csv file
df = pd.read_csv('fileName.csv')


#loop over columns and replace nans with the most recent value available
for c in df.columns:
    df[c].fillna(method='ffill', inplace=True)

#write out your df back to csv
df.to_csv('newFile.csv', index=False)