简体   繁体   中英

Linear interpolation between rows in pandas DataFrame

I have the following DataFrame:

import numpy as np
import pandas as pd

df = pd.DataFrame([np.zeros(10),
               np.arange(1,11),
               np.arange(11,21),
               np.arange(21,31)])

I'm looking for a way to do efficient linear interpolation between the rows. For example, I want to add n new rows between each row in the DataFrame using linear interpolation. The method needs to be as efficient as possible as the actual DataFrames I am using are quite large. The expected output is a DataFrame with n new rows between each existing row.

IIUC, you can use reindex with np.linspace if your index is default range index, otherwise you can reset_index use this method.

df = pd.DataFrame([np.zeros(10),
               np.arange(1,11),
               np.arange(11,21),
               np.arange(21,31)])

n=5
new_indx = np.linspace(df.index.min(), df.index.max(), (df.shape[0]-1)*n+df.shape[0])
df.reindex(new_indx).interpolate(method='index')

Output:

                  0          1          2          3          4          5  \
0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   
0.166667   0.166667   0.333333   0.500000   0.666667   0.833333   1.000000   
0.333333   0.333333   0.666667   1.000000   1.333333   1.666667   2.000000   
0.500000   0.500000   1.000000   1.500000   2.000000   2.500000   3.000000   
0.666667   0.666667   1.333333   2.000000   2.666667   3.333333   4.000000   
0.833333   0.833333   1.666667   2.500000   3.333333   4.166667   5.000000   
1.000000   1.000000   2.000000   3.000000   4.000000   5.000000   6.000000   
1.166667   2.666667   3.666667   4.666667   5.666667   6.666667   7.666667   
1.333333   4.333333   5.333333   6.333333   7.333333   8.333333   9.333333   
1.500000   6.000000   7.000000   8.000000   9.000000  10.000000  11.000000   
1.666667   7.666667   8.666667   9.666667  10.666667  11.666667  12.666667   
1.833333   9.333333  10.333333  11.333333  12.333333  13.333333  14.333333   
2.000000  11.000000  12.000000  13.000000  14.000000  15.000000  16.000000   
2.166667  12.666667  13.666667  14.666667  15.666667  16.666667  17.666667   
2.333333  14.333333  15.333333  16.333333  17.333333  18.333333  19.333333   
2.500000  16.000000  17.000000  18.000000  19.000000  20.000000  21.000000   
2.666667  17.666667  18.666667  19.666667  20.666667  21.666667  22.666667   
2.833333  19.333333  20.333333  21.333333  22.333333  23.333333  24.333333   
3.000000  21.000000  22.000000  23.000000  24.000000  25.000000  26.000000   

                  6          7          8          9  
0.000000   0.000000   0.000000   0.000000   0.000000  
0.166667   1.166667   1.333333   1.500000   1.666667  
0.333333   2.333333   2.666667   3.000000   3.333333  
0.500000   3.500000   4.000000   4.500000   5.000000  
0.666667   4.666667   5.333333   6.000000   6.666667  
0.833333   5.833333   6.666667   7.500000   8.333333  
1.000000   7.000000   8.000000   9.000000  10.000000  
1.166667   8.666667   9.666667  10.666667  11.666667  
1.333333  10.333333  11.333333  12.333333  13.333333  
1.500000  12.000000  13.000000  14.000000  15.000000  
1.666667  13.666667  14.666667  15.666667  16.666667  
1.833333  15.333333  16.333333  17.333333  18.333333  
2.000000  17.000000  18.000000  19.000000  20.000000  
2.166667  18.666667  19.666667  20.666667  21.666667  
2.333333  20.333333  21.333333  22.333333  23.333333  
2.500000  22.000000  23.000000  24.000000  25.000000  
2.666667  23.666667  24.666667  25.666667  26.666667  
2.833333  25.333333  26.333333  27.333333  28.333333  
3.000000  27.000000  28.000000  29.000000  30.000000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM