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?
-
1Please provide data as plain text, not images. Also, do you want a python/pandas solution, an R solution or both?neilfws– neilfws2017-10-30 22:09:29 +00:00Commented Oct 30, 2017 at 22:09
-
3Possible duplicate of How to replace NaNs by preceding values in pandas DataFrame?0TTT0– 0TTT02017-10-30 22:11:54 +00:00Commented Oct 30, 2017 at 22:11
-
1Please do not post images of code or data.wwii– wwii2017-10-30 22:16:16 +00:00Commented 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),hrbrmstr– hrbrmstr2017-10-30 23:31:27 +00:00Commented Oct 30, 2017 at 23:31
Add a comment
|
2 Answers
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)
3 Comments
code
Thank you Wen! It is working and I learned something which I didn't know. May you please elaborate the code to me!
BENY
@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 valuecode
@ Wen Understood, Thank you!