简体   繁体   English

Pandas 为每个单元格添加行,其中 function 应用于该特定单元格,而新行的其他元素等于起始行

[英]Pandas add row for each cell where function is applied to that specific cell while other element of new row are equal to starting row

I want to loop through each element of a pandas dataframe row such that only that element is stressed (ie: it's multiplied by 10%) while the other elements of the row are kept equal.我想遍历 pandas dataframe 行的每个元素,这样只有该元素被强调(即:它乘以 10%),而该行的其他元素保持相等。

I'm planning to use this for sensitivity analysis.我打算用它来进行敏感性分析。

Example: df = pd.DataFrame({'AGE':[5,10],'POP':[100,200]})示例: df = pd.DataFrame({'AGE':[5,10],'POP':[100,200]})

AGE年龄 POP流行音乐
5 5个 100 100
10 10 200 200

Final desired output:最终需要 output:

AGE年龄 POP流行音乐
5 5个 100 100
10 10 200 200
5*1.1 5*1.1 100 100
5 5个 100*1.1 100*1.1
10*1.1 10*1.1 200 200
10 10 200*1.1 200*1.1

You can use a cross merge andconcat :您可以使用交叉mergeconcat

pd.concat([df, 
           (df.merge(pd.Series([1.1, 1], name='factor'), how='cross')
              .pipe(lambda d: d.mul(d.pop('factor'), axis=0))
            )], ignore_index=True)

Output: Output:

    AGE    POP
0   5.0  100.0
1  10.0  200.0
2   5.5  110.0
3   5.0  100.0
4  11.0  220.0
5  10.0  200.0

If you have 2 columns, you can multiply with the [1, stress] and its reverse those columns, concatanate them while sorting to preserve multiplied column order.如果您有 2 列,您可以乘以 [1, stress] 并反转这些列,在排序时连接它们以保留相乘的列顺序。 Lastly, prepend the original frame as well:最后,还要加上原始帧:

stress = 1.1
factor = [stress, 1]
pd.concat([df,
           pd.concat([df.mul(factor),
                      df.mul(factor[::-1])]).sort_index()
          ], ignore_index=True)

    AGE    POP
0   5.0  100.0
1  10.0  200.0
2   5.5  100.0
3   5.0  110.0
4  11.0  200.0
5  10.0  220.0

Generalizing to N columns could be via a comprehension:推广到 N 列可以通过理解:

def gen_factors(stress, N):
    for j in range(N):
        # make all-1s list, except j'th is `stress`
        f = [1] * N
        f[j] = stress
        yield f

stress = 1.1
N = len(df.columns)
pd.concat([df,
           pd.concat(df.mul(factor)
                     for factor in gen_factors(stress, N)).sort_index()
          ], ignore_index=True)

Example run for a 3-column frame: 3 列框架的示例运行:

>>> df

   AGE  POP  OTHER
0    5  100      7
1   10  200      8

>>> # output of above:

    AGE    POP  OTHER
0   5.0  100.0    7.0
1  10.0  200.0    8.0
2   5.5  100.0    7.0
3   5.0  110.0    7.0
4   5.0  100.0    7.7
5  11.0  200.0    8.0
6  10.0  220.0    8.0
7  10.0  200.0    8.8

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

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