简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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