Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

python - How to fill in missing values in Pandas dataframe according to pattern in column?

Suppose I have a dataframe with a column as follows:

Column
10
NaN
20
NaN
30

I want each row to be filled in with increments of 5 so that the final output would appear like:

Column
10
15
20
25
30

I've tried using np.arange and .reindex() but haven't had much luck. I'm looking for an iterative approach instead of simply manually filling in. Can anyone please help with this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try with interpolate

df['Column']=df.Column.interpolate()
Out[86]: 
0    10.0
1    15.0
2    20.0
3    25.0
4    30.0
Name: Column, dtype: float64

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...