简体   繁体   English

将多列唯一的计算值添加到熊猫数据框

[英]Adding multiple columns of unique calculated values to a pandas dataframe

I would like to add new columns to a data frame using function and values from the original data frame 我想使用原始数据框中的函数和值向数据框中添加新列

Create the dataframe 创建数据框

df = pd.DataFrame({'f1'   : np.random.randn(10), 
                   'f2'   : np.random.randn(10),
                   'f3'   : np.random.randn(10),
                   'f4'   : np.random.randn(10),
                   'f5'   : np.random.randn(10)})

Test function to be applied to the existing columns 测试功能将应用于现有列

def testfun(x,n):
    return x * n

Arguments for the function - each new column has different arguments 函数的参数-每个新列都有不同的参数

colnum   = [1,2,3,4,5]

Create new columns names for the new columns to add to the data frame 为新列创建新列名称以添加到数据框中

newcol         = [s + "_D" for s in df.columns]

Loop through the existing columns applying the function and appropriate argument for that column. 在现有列中循环应用该列的函数和适当的参数。 Each new column will be assigned an unique name. 每个新列将被分配一个唯一的名称。

This part function does not work! 这部分功能不起作用!

for s in range(len(df.columns)):    
     df       = df.assign(newcol[s] = testfun(df[[df.columns[s]]], s))

The new data frame should contain 10 columns. 新的数据框应包含10列。

You can try this 你可以试试这个

import pandas as pd
import numpy as np

df = pd.DataFrame({'f1'   : np.random.randn(10), 
                   'f2'   : np.random.randn(10),
                   'f3'   : np.random.randn(10),
                   'f4'   : np.random.randn(10),
                   'f5'   : np.random.randn(10)})

def testfun(x,n):
    return x * n

colnum = [1,2,3,4,5]
newcol = [s + "_D" for s in df.columns]
for s in range(len(df.columns)):    
    df.loc[:,newcol[s]] = df[[df.columns[s]]]*s

IIUC 联合会

df.join(df.mul([1, 2, 3, 4, 5]).add_suffix('_D'))

         f1        f2        f3        f4        f5      f1_D      f2_D      f3_D      f4_D      f5_D
0 -1.036309  0.094285  1.296874  1.154490  0.034166 -1.036309  0.188570  3.890622  4.617961  0.170830
1 -1.015998  0.180154 -0.332444  2.007173 -0.807009 -1.015998  0.360308 -0.997331  8.028691 -4.035047
2  0.125456 -0.758892 -0.028901 -2.053950  0.665908  0.125456 -1.517783 -0.086704 -8.215801  3.329542
3 -1.097128  1.455765 -0.336339  1.076013  0.714174 -1.097128  2.911530 -1.009017  4.304054  3.570869
4 -0.314902 -1.148362 -0.123719 -0.055161  0.436508 -0.314902 -2.296724 -0.371157 -0.220642  2.182539
5  0.718147 -0.029205 -0.649937 -1.046908 -0.965463  0.718147 -0.058410 -1.949812 -4.187633 -4.827316
6  0.454862  0.218846 -0.185591 -0.105686 -1.459477  0.454862  0.437693 -0.556774 -0.422745 -7.297384
7  0.613794 -2.635875 -0.083078  1.180391 -0.256504  0.613794 -5.271751 -0.249233  4.721563 -1.282521
8  0.283366  0.256394 -0.222937  1.171640  0.351768  0.283366  0.512789 -0.668811  4.686561  1.758840
9  0.653707 -0.336425  0.328433 -0.460891 -0.631990  0.653707 -0.672851  0.985298 -1.843564 -3.159948

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

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