Python Forum
Interpolating DataFrame method=‘index’ help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Interpolating DataFrame method=‘index’ help (/thread-30459.html)



Interpolating DataFrame method=‘index’ help - tlewick1 - Oct-21-2020

Given the following:
import pandas as pd
merged = pd.DataFrame({'Z':(0,1.,2.5,3.),'X':(-1.,None,None,0),'U':(0,1.,1.,0)})

for col in merged:
    merged[col] = pd.to_numeric(merged[col], errors='coerce')

print(pd.Series(data=merged['X'].values,index=merged['Z'].values)
    .interpolate(method='index'))
print('\nvs.\n',merged.interpolate(method='linear'))
Can I interpolate my data within the DataFrame similar to the 'linear' method but get the results achieved by indexing a series? I'm losing data by making the series and don't know how to get my answer otherwise.

Do I need to take my DataFrame, break it up into Series, then turn them back into a new interpolated DataFrame?


RE: Interpolating DataFrame method=‘index’ help - scidam - Oct-22-2020

You can set index, perform interpolation and drop the index:

merged.set_index('Z').interpolate(method='index').reset_index()