Home > database >  Fill NaN values with values from previous records with data (based on date field)
Fill NaN values with values from previous records with data (based on date field)

Time:04-27

I have the following dataframe:

          open        high         low       close    adjclose    volume       date  polarity  subjectivity
0   277.559998  282.500000  272.059998  282.190002  282.190002  42549400 2022-03-28       NaN           NaN
1   286.950012  289.459991  279.799988  286.559998  286.559998  48898400 2022-03-29  0.160732      0.438396
2   283.040009  284.959991  275.029999  276.899994  276.899994  46348700 2022-03-30       NaN           NaN
3   277.820007  282.480011  272.700012  272.859985  272.859985  52344000 2022-03-31       NaN           NaN
4   273.750000  274.959991  262.670013  267.119995  267.119995  51653400 2022-04-01       NaN           NaN
5   267.279999  275.579987  266.130005  273.600006  273.600006  39712000 2022-04-04       NaN           NaN
6   272.540009  273.190002  258.200012  259.309998  259.309998  43661500 2022-04-05  0.170833      0.479167
7   249.339996  253.000000  240.029999  244.070007  244.070007  70383300 2022-04-06       NaN           NaN
8   244.410004  247.220001  234.779999  242.080002  242.080002  55799200 2022-04-07       NaN           NaN

I would like to fill the NaN values with data with a date that is older, leaving NaN values in those who don't have any previous record with data, to get this result:

          open        high         low       close    adjclose    volume       date  polarity  subjectivity
0   277.559998  282.500000  272.059998  282.190002  282.190002  42549400 2022-03-28       NaN           NaN
1   286.950012  289.459991  279.799988  286.559998  286.559998  48898400 2022-03-29  0.160732      0.438396
2   283.040009  284.959991  275.029999  276.899994  276.899994  46348700 2022-03-30  0.160732      0.438396
3   277.820007  282.480011  272.700012  272.859985  272.859985  52344000 2022-03-31  0.160732      0.438396
4   273.750000  274.959991  262.670013  267.119995  267.119995  51653400 2022-04-01  0.160732      0.438396
5   267.279999  275.579987  266.130005  273.600006  273.600006  39712000 2022-04-04  0.160732      0.438396
6   272.540009  273.190002  258.200012  259.309998  259.309998  43661500 2022-04-05  0.170833      0.479167
7   249.339996  253.000000  240.029999  244.070007  244.070007  70383300 2022-04-06  0.170833      0.479167
8   244.410004  247.220001  234.779999  242.080002  242.080002  55799200 2022-04-07  0.170833      0.479167

CodePudding user response:

Use forward fill: ffill:

df = df.ffill()

CodePudding user response:

Solution to replace NaN values in a dataframe is to use the function fillna() as following Ex:

df.fillna('',inplace=True)

Or

df[index] = df[index].fillna('')

  • Related