在 Python 中分配多个列的任何更简单的方法,如 R data.table :=

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

我想知道是否有更简单的方法在 Python 中分配多个列,就像 R data.table.

中的 :=

例如,在Python中我必须这样写:

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

然而,它只是R中的一行代码data.table:

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

这样的东西对你有用吗:

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))

产生

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

尽管有人会争辩说将代码写在多行上会使其更具可读性。

是的,在 python 到

中有一个更简单的方法
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"]]

您可以使用DataFrame.assign分配多个列:

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)

示例:

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

如果你想要带空格的列名,你可以使用 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