0

I have a dataset like this:

import pandas as pd
import numpy as np

df = pd.DataFrame({'Name': ['A', 'B', 'C', 'A', 'B'], 'City': [1, 3, 5, np.nan, np.nan], 'Club': [2, 4, 6, np.nan, np.nan], 'Date': ['x', 'y', 'z', 'n', 'g']})

Name    City    Club     Date
 A       1        2        x
 B       3        4        y
 C       5        6        z
 A      NaN      NaN       n
 B      NaN      NaN       g

How do I use values from "A" and "B" in previous rows to replace NaN in the new rows?

Expected:

Name    City    Club     Date
 A       1        2        x
 B       3        4        y
 C       5        6        z
 A       1        2        n
 B       3        4        g

So far I tired df['City'].fillna( method ='ffill', inplace = True), but it only allows me to fill missing value one column.

If I use df['City','Club'].fillna( method ='ffill', inplace = True) I got KeyError: ('City', 'Club').

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
benbenaj
  • 1
  • 2

0 Answers0