在多个列上应用函数以创建多个新列 [英] Applying function on multiple columns to create multiple new columns

查看:53
本文介绍了在多个列上应用函数以创建多个新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在df上应用函数以创建多个新列.作为我功能的输入,我需要(i)行或(ii)多列

I need to apply a function on a df to create multiple new columns. As an input to my function I would need (i) a row or (ii) multiple columns

def divideAndMultiply(x,y):
    return x/y, x*y

df["e"], df["f"] = zip(*df.a.apply(lambda val: divideAndMultiply(val,2)))

( https://stackoverflow.com/a/36600318/1831695 )

这可以创建多个新列,但不能创建多个输入(=该df中的列).还是我错过了什么?

This works to create multiple new columns, but not with multiple inputs (= columns from that df). Or did I miss something?

举例说明我想做什么:

我的DF有几列.其中3个(a,b,c)与计算两个新列(y,z)有关,其中y = a + b + cz = c - b - a

My DF has several columns. 3 (a, b, c) of them are relevant to calculate two new columns (y, z) where, y = a + b + c and z = c - b - a

我知道这是一个简单的计算,不需要使用函数,但是假设我们需要一个函数. 我不想只编写和应用2个函数,而只希望有一个函数,返回两个值并接受所有三个值(甚至更好:一行)进行计算.

I know this is an easy calculation where I do not need a function, but let's assume we would need one. Instead of writing and applying 2 functions, I would like to have only one function, return both values and accepting all three values (or even better: a row) for the calculation.

此示例:

df["y"], df["z"] = zip(*df.a.apply(lambda val: divideAndMultiply(val,2)))

仅在使用一个列数据项(val)和其他值(在这种情况下为2)时有效.

works only when using one column data item (val) and on other value (2 in this case).

我需要这样的东西:

df["y"], df["z"] = zip(*df.a.apply(lambda val: divideAndMultiply(df['a'],df['b'],df['c'])))

(是的,我知道val被分配给df.a)

(And yes, I know that val is assigned to df.a )

更新2 这就是我的df的样子:

Update 2 this is how my df looks like:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 4655 entries, 0 to 4654
Data columns (total 71 columns):
Open                  4655 non-null float64
Close                 4655 non-null float64
High                  4655 non-null float64
Low                   4655 non-null float64
DateTime              4655 non-null datetime64[ns]
Date                  4655 non-null datetime64[ns]
T_CLOSE               4655 non-null float64
T_HIGH                4655 non-null float64
T_LOW                 4655 non-null float64
T_OPEN                4655 non-null float64
MVA5                  4651 non-null float64
MVA10                 4646 non-null float64
MVA14                 4642 non-null float64
MVA15                 4641 non-null float64
MVA28                 4628 non-null float64
MVA50                 4606 non-null float64
STD5                  4651 non-null float64
STD10                 4646 non-null float64
STD15                 4641 non-null float64
CV_5                  4651 non-null float64
CV_10                 4646 non-null float64
CV_15                 4641 non-null float64
DIFF_VP_CLOSE         4654 non-null float64
DIFF_VP_HIGH          4654 non-null float64
DIFF_VP_OPEN          4654 non-null float64
DIFF_VP_LOW           4654 non-null float64
AVG_STEIG_5           4650 non-null float64
AVG_STEIG_10          4645 non-null float64
AVG_STEIG_15          4640 non-null float64
AVG_STEIG_28          4627 non-null float64
AVG_5_DIFF            4651 non-null float64
AVG_10_DIFF           4646 non-null float64
AVG_15_DIFF           4641 non-null float64
AVG_14_DIFF           4642 non-null float64
AVG_50_DIFF           4606 non-null float64
AD_5_14               4642 non-null float64
Momentum_4            4651 non-null float64
ROC_4                 4652 non-null float64
Momentum_8            4647 non-null float64
ROC_8                 4648 non-null float64
Momentum_12           4643 non-null float64
ROC_12                4644 non-null float64
VT_OPEN               4598 non-null float64
VT_CLOSE              4598 non-null float64
VT_HIGH               4598 non-null float64
VT_LOW                4598 non-null float64
PP_VT                 4598 non-null float64
R1_VT                 4598 non-null float64
R2_VT                 4598 non-null float64
R3_VT                 4598 non-null float64
S1_VT                 4598 non-null float64
S2_VT                 4598 non-null float64
S3_VT                 4598 non-null float64
DIFF_VT_CLOSE         4598 non-null float64
DIFF_VT_HIGH          4598 non-null float64
DIFF_VT_OPEN          4598 non-null float64
DIFF_VT_LOW           4598 non-null float64
DIFF_T_OPEN           4655 non-null float64
DIFF_T_LOW            4655 non-null float64
DIFF_T_HIGH           4655 non-null float64
DIFF_T_CLOSE          4655 non-null float64
DIFF_VTCLOSE_TOPEN    4598 non-null float64
VP_HIGH               4654 non-null float64
VP_LOW                4654 non-null float64
VP_OPEN               4654 non-null float64
VP_CLOSE              4654 non-null float64
regel_r1              4655 non-null int64
regel_r2              4655 non-null int64
regel_r3              4655 non-null int64
regeln                4655 non-null int64
vormittag_flag        4655 non-null int64
dtypes: datetime64[ns](2), float64(64), int64(5)
memory usage: 2.6 MB
None

         Open    Close     High      Low            DateTime       Date  T_CLOSE   T_HIGH    T_LOW   T_OPEN      MVA5     MVA10         MVA14         MVA15         MVA28      MVA50      STD5     STD10     STD15      CV_5     CV_10     CV_15  DIFF_VP_CLOSE  DIFF_VP_HIGH  DIFF_VP_OPEN  DIFF_VP_LOW  AVG_STEIG_5  AVG_STEIG_10  AVG_STEIG_15  AVG_STEIG_28  AVG_5_DIFF  AVG_10_DIFF  AVG_15_DIFF  AVG_14_DIFF  AVG_50_DIFF   AD_5_14  Momentum_4    ROC_4  Momentum_8     ROC_8  Momentum_12    ROC_12  VT_OPEN  VT_CLOSE  VT_HIGH   VT_LOW         PP_VT         R1_VT         R2_VT         R3_VT         S1_VT         S2_VT         S3_VT  DIFF_VT_CLOSE  DIFF_VT_HIGH  DIFF_VT_OPEN  DIFF_VT_LOW  DIFF_T_OPEN  DIFF_T_LOW  DIFF_T_HIGH  DIFF_T_CLOSE  DIFF_VTCLOSE_TOPEN  VP_HIGH   VP_LOW  VP_OPEN  VP_CLOSE  regel_r1  regel_r2  regel_r3  regeln  vormittag_flag   T_DIRC T_WECHSELC  T_NUM_INNEN T_CANDLEC
4653  12488.1  12490.1  12490.6  12484.9 2017-05-03 14:00:00 2017-05-03  12490.1  12508.3  12475.4  12506.5  12490.18  12488.41  12487.521429  12487.053333  12493.078571  12498.118  2.178761  4.334218  4.515033  0.000174  0.000347  0.000362           -1.7           2.5          -1.0         -2.7         -9.6           8.5      4.866667     -8.178571       -0.08         1.69     3.046667     2.578571       -8.018 -2.658571        -3.8  0.00004         8.7  0.000577         -0.3  0.000521  12449.3   12514.3  12527.8  12432.0  12491.366667  12550.733333  12587.166667  12646.533333  12454.933333  12395.566667  12359.133333           24.2          37.7         -40.8        -58.1         16.4       -14.7         18.2           0.0                 7.8  12492.6  12487.4  12489.1   12488.4         0         0         0       0               0  neutral      INNEN            1     GRUEN
4654  12489.9  12489.9  12489.9  12489.6 2017-05-03 14:15:00 2017-05-03  12489.9  12508.3  12475.4  12506.5  12489.38  12488.91  12487.828571  12487.680000  12492.182143  12498.436  0.712039  4.169586  4.180431  0.000057  0.000334  0.000335            0.2           0.7          -1.8         -5.0         -8.0           5.0      6.266667     -8.964286        0.52         0.99     2.220000     2.071429       -8.536 -1.551429         0.3  0.00008         7.0  0.000064          6.3  0.000665  12449.3   12514.3  12527.8  12432.0  12491.366667  12550.733333  12587.166667  12646.533333  12454.933333  12395.566667  12359.133333           24.4          37.9         -40.6        -57.9         16.6       -14.5         18.4           0.0                 7.8  12490.6  12484.9  12488.1   12490.1         0         0         0       0               0  neutral      INNEN            1     GRUEN

推荐答案

更新

这是一个简单的示例,其中包含 Quandl 数据集,

import quandl

df = quandl.get("WIKI/GOOGL")
columns = ["High", 'Low', 'Close']

def operations(row, columns):

  df1 = row[columns[0]] + row[columns[1]] + row[columns[2]]
  df2 = -row[columns[1]] - row[columns[2]] + row[columns[0]]
  return df1, df2

df["function1"], df["function2"] = zip(*df.apply(lambda row: operations(row, columns), axis=1))

df[["High","Low","Close","function1", "function2"]].head(5)

这篇关于在多个列上应用函数以创建多个新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆