Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16803

Fill N/A with previous day data

$
0
0

I have a dataframe and it just has data for weekday. Below is sample dataframe:

import pandas as pdimport numpy as npdf = pd.DataFrame({'BAS_DT': ['2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05', '2023-01-05', '2023-01-05', '2023-01-06', '2023-01-07'], 'CUS_NO': [np.nan, np.nan, '900816636', '900816636', '900816946', '900816931', np.nan, np.nan]})df    BAS_DT      CUS_NO0   2023-01-02  NaN1   2023-01-03  NaN2   2023-01-04  9008166363   2023-01-05  9008166364   2023-01-05  9008169465   2023-01-05  9008169316   2023-01-06  NaN7   2023-01-07  NaN

I want to fill 2023-01-06 and 2023-01-07 same with 2023-01-05. I tried ffill but it just fill with the first row that closest to NaN row. Below is my desired Output:

    BAS_DT      CUS_NO0   2023-01-02  NaN1   2023-01-03  NaN2   2023-01-04  9008166363   2023-01-05  9008166364   2023-01-05  9008169465   2023-01-05  9008169316   2023-01-06  9008166367   2023-01-06  9008169468   2023-01-06  9008169319   2023-01-07  90081663610  2023-01-07  90081694611  2023-01-07  900816931       

Thank you.


Viewing all articles
Browse latest Browse all 16803

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>