0

MY DataFrame contains several data for each date. in my date column date has entered only for the first data of the day, for rest of the data of the day there is only sparse value. How can I fill all the all the unfilled date values with corresponding date?

Following is the snippet of my data frame

4
  • 1
    Please provide data as plain text, not images. Also, do you want a python/pandas solution, an R solution or both? Commented Oct 30, 2017 at 22:09
  • 3
    Possible duplicate of How to replace NaNs by preceding values in pandas DataFrame? Commented Oct 30, 2017 at 22:11
  • 1
    Please do not post images of code or data. Commented Oct 30, 2017 at 22:16
  • Spamming tags for 👀 is not cool. "pandas" has nothing to do with R (and I'm being kind by not referring maliciously to python-related ilk), Commented Oct 30, 2017 at 23:31

2 Answers 2

3

In Python

df['Date']=df['Date'].replace({'':np.nan}).ffill()

In R

library(zoo)
df$Date[df$Date=='']=NA
df$Date=na.locf(df$Date)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Wen! It is working and I learned something which I didn't know. May you please elaborate the code to me!
@DharitParmar Basically R and Python are same here, replace the blank with np.nan, ffill and na.locf is to fill the na by using the previous not null value
@ Wen Understood, Thank you!
3

You can use fillna function.

# Say df is your dataframe
# To fill values forward use:
df.fillna(method='ffill') 

# To fill values backward use:
df.fillna(method='bfill')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.