Reputation: 133
I was wondering how I could interpolate between rows for the column 'Data' where I loop through it and create a new row with '0.3', '0.5' and '0.6' to fill in the gaps?
df = pd.DataFrame([0.1,0.2,0.4,0.7],columns=['Data'])
df
How do I do this with code to the original dataframe to produce the image below?
Thanks a lot in advance.
Upvotes: 1
Views: 198
Reputation: 863791
In my opinion there should be 2 solutions, also added more columns to sample data:
df = pd.DataFrame({'Data': [0.1,0.2,0.4,0.7],
'A':list('abcd'),
'B':[4,5,6,9]})
print (df)
Data A B
0 0.1 a 4
1 0.2 b 5
2 0.4 c 6
3 0.7 d 9
Create index by Data
column and use DataFrame.reindex
:
arr = np.arange(df.Data.min(),df.Data.max() + 0.1,0.1)
df1 = df.set_index('Data').reindex(arr).reset_index()
print (df1)
Data A B
0 0.1 a 4.0
1 0.2 b 5.0
2 0.3 NaN NaN
3 0.4 c 6.0
4 0.5 NaN NaN
5 0.6 NaN NaN
6 0.7 NaN NaN
Or create helper DataFrame
and DataFrame.merge
with left join:
df1 = pd.DataFrame({'Data':arr}).merge(df, how='left')
print (df1)
Data A B
0 0.1 a 4.0
1 0.2 b 5.0
2 0.3 NaN NaN
3 0.4 c 6.0
4 0.5 NaN NaN
5 0.6 NaN NaN
6 0.7 NaN NaN
Upvotes: 2