简体   繁体   English

如何根据行值将 function 应用于 dataframe

[英]How to apply function to dataframe based on row value

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.列 Value1 包含数字 1 到 100 的三元组。列 Value2 包含随机数。 I am wondering how to apply a function for each set of triplicates and append their results together.我想知道如何将 function 和 append 它们的结果一起应用。

For example, consider an example function called "Interpolate", which linearly interpolates each group of triplicates into five rows, affecting only the column Value2.例如,考虑一个名为“Interpolate”的示例 function,它将每组三元组线性插入五行,仅影响列 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
...        ...

You can groupby "Value1" and apply a function to each group:您可以按“Value1”分组并将groupby应用于每个组:

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: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据特定列中的值将函数应用于熊猫中的数据框行 - Apply function to dataframe row in pandas based on value in specific column 如何根据条件和另一行的值将 function 应用于 dataframe 行? - How to apply a function to a dataframe row based on a condition and values of another row? 如何将上一行的值传递给 dataframe 应用 function? - How to pass the value of previous row to the dataframe apply function? 根据同一行的其他列中的值将函数应用于dataframe列元素? - Apply function to dataframe column element based on value in other column for same row? 如何在 dataframe 的每一行上应用 function? - How to apply a function on every row on a dataframe? 如何将我的 function 应用到 dataframe 的第一行? - How to apply my function to the first row of a dataframe? 熊猫数据框应用功能基于选定的行创建新列 - Pandas dataframe apply function to create new column based on selected row 如何将返回 dataframe 的 function 应用到另一个 dataframe 的每一行 - how to apply a function that returns a dataframe to each row of another dataframe 如何使用python apply/lambda/shift函数根据2列的值获取该特定列的前一行值? - How to use python apply/lambda/shift function to get the previous row value of that particular column based on the value of 2 columns? 将功能应用于数据框中的特定行 - Apply function to specific row in dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM