Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading