Any simpler way to assign multiple columns in Python like R data.table :=

Question:

I’m wondering if there’s any simpler way to assign multiple columns in Python, just like the := in R data.table.

For example, in Python I would have to write like this:

df['Col_A'] = df.A/df.B
df['Col_B'] = df.C/df.D
df['Col_C'] = df.E/df.F * 1000000    
df['Col_D'] = df.G/df.H * 1000000

However, it’s just one line of code in R data.table:

df[, ':='(Col_A = A/B, Col_B = C/D, Col_C = E/F*1000000, Col_B = G/H*1000000)]
Asked By: Jiamei

||

Answers:

Would something like this work for you:

import pandas as pd

df = pd.DataFrame()

# fake data
a = [1,2,3]
b = None, None, None
c = True, False, True

# assign to df
df[["a", "b", "c"]] = np.asarray([a, b, c]).transpose((1, 0))

which yields

   a     b      c
0  1  None   True
1  2  None  False
2  3  None   True

Although one would argue that writing the code on multiple line makes it more readable.

Answered By: Kevin Hillairet

yes there is a simpler way in python too

import pandas as  pd
df = pd.DataFrame({"x":[1,2,3],"y":[4,5,6]})
df[["pp","ll","nn"]]= [df["x"]/df["y"],df["x"],df["x"]]
Answered By: Sadaf Shafi

You can use DataFrame.assign to assign multiple columns:

df = df.assign(Col_A=df.A/df.B, Col_B=df.C/df.D, Col_C=df.E/df.F*1000000, Col_D=df.G/df.H*1000000)

Example:

df = pd.DataFrame(np.random.random((4, 8)), columns=list('ABCDEFGH'))

#           A         B  ...         H
# 0  0.771211  0.238201  ...  0.311904
# 1  0.197548  0.635218  ...  0.626639
# 2  0.332333  0.838589  ...  0.477978
# 3  0.929690  0.327412  ...  0.046179
df = df.assign(Col_A=df.A/df.B, Col_B=df.C/df.D, Col_C=df.E/df.F*1000000, Col_D=df.G/df.H*1000000)

#           A         B  ...         H     Col_A     Col_B         Col_C         Col_D
# 0  0.771211  0.238201  ...  0.311904  3.237647  1.547285  1.463586e+06  2.845234e+06
# 1  0.197548  0.635218  ...  0.626639  0.310993  1.385892  1.394466e+07  2.685293e+05
# 2  0.332333  0.838589  ...  0.477978  0.396300  0.078238  8.494174e+06  6.001031e+05
# 3  0.929690  0.327412  ...  0.046179  2.839514  0.852443  1.962892e+06  8.791233e+06

If you want column names with spaces, you can use a dict:

df = df.assign(**{'Col A': df.A/df.B, 'Col B': df.C/df.D, 'Col C': df.E/df.F*1000000, 'Col D': df.G/df.H*1000000})

#           A         B  ...         H      Col A     Col B         Col C         Col D
# 0  0.868320  0.086743  ...  0.505330  10.010311  6.680195  1.147554e+06  2.620416e+05
# 1  0.244341  0.908793  ...  0.389684   0.268863  2.388179  2.196769e+06  2.235063e+06
# 2  0.917949  0.248149  ...  0.710027   3.699188  0.453094  1.311617e+06  1.004200e+06
# 3  0.616655  0.498817  ...  0.703579   1.236235  2.186589  1.272981e+06  8.602272e+05
Answered By: tdy