Forward fill NaNs
Python
import pandas as pd
import numpy as np
clients = {
'client_id' : [1001, 1001, 1001, 1002, 1002, 1002, 1003, 1003],
'ranking' : [1, 2, 3, 1, 2, 3, 1, 2],
'value': [1000, np.nan, 1200, 1500, 1250, np.nan, 1100, np.nan]
}
clients_df = pd.DataFrame(clients)
def previous_nan_values(clients_df):
clients_df = (
clients_df.sort_values(['client_id', 'ranking'])
.fillna(method = 'ffill')
.sort_values(['ranking', 'client_id'])
)
return clients_df
print(previous_nan_values(clients_df))
Output
Embed on website
To embed this program on your website, copy the following code and paste it into your website's HTML:
Comments
This comment belongs to a banned user and is only visible to admins.
This comment belongs to a deleted user and is only visible to admins.