Home > Back-end >  How to apply function to dataframe based on row value
How to apply function to dataframe based on row value

Time:01-16

Consider the table below:

Value1     Value2
1          1    
1          2
1          3
2          7
2          8    
2          9
...        ...
100        1
100        2
100        3

The column Value1 contains sets of triplicates for numbers 1 to 100. The column Value2 contains random numbers. I am wondering how to apply a function for each set of triplicates and append their results together.

For example, consider an example function called "Interpolate", which linearly interpolates each group of triplicates into five rows, affecting only the column Value2. The result would be:

Value1     Value2
1          1    
1          1.5
1          2
1          2.5
1          3    
2          7
2          7.5
2          8
2          8.5
2          9
...        ...

CodePudding user response:

You can groupby "Value1" and apply a function to each group:

def interpolate(x):
    # change index to even numbers
    x.index = range(0, 2*len(x), 2)
    # add NaN rows for odd indices
    x = x.reindex(range(2*len(x)-1))
    # interpolate
    x = x.interpolate()
    return x

out = df.groupby('Value1').apply(interpolate).droplevel(0)

The above code as a one-liner:

out = df.groupby('Value1').apply(lambda x: x.set_axis(range(0, 2*len(x), 2)).reindex(range(2*len(x)-1))).interpolate().droplevel(0)

Output:

   Value1  Value2
0     1.0     1.0
1     1.0     1.5
2     1.0     2.0
3     1.0     2.5
4     1.0     3.0
0     2.0     7.0
1     2.0     7.5
2     2.0     8.0
3     2.0     8.5
4     2.0     9.0
0   100.0     1.0
1   100.0     1.5
2   100.0     2.0
3   100.0     2.5
4   100.0     3.0
  •  Tags:  
  • Related