Apply pandas function to column to create multiple new columns? Apply pandas function to column to create multiple new columns? python python

Apply pandas function to column to create multiple new columns?


I usually do this using zip:

>>> df = pd.DataFrame([[i] for i in range(10)], columns=['num'])>>> df    num0    01    12    23    34    45    56    67    78    89    9>>> def powers(x):>>>     return x, x**2, x**3, x**4, x**5, x**6>>> df['p1'], df['p2'], df['p3'], df['p4'], df['p5'], df['p6'] = \>>>     zip(*df['num'].map(powers))>>> df        num     p1      p2      p3      p4      p5      p60       0       0       0       0       0       0       01       1       1       1       1       1       1       12       2       2       4       8       16      32      643       3       3       9       27      81      243     7294       4       4       16      64      256     1024    40965       5       5       25      125     625     3125    156256       6       6       36      216     1296    7776    466567       7       7       49      343     2401    16807   1176498       8       8       64      512     4096    32768   2621449       9       9       81      729     6561    59049   531441


Building off of user1827356 's answer, you can do the assignment in one pass using df.merge:

df.merge(df.textcol.apply(lambda s: pd.Series({'feature1':s+1, 'feature2':s-1})),     left_index=True, right_index=True)    textcol  feature1  feature20  0.772692  1.772692 -0.2273081  0.857210  1.857210 -0.1427902  0.065639  1.065639 -0.9343613  0.819160  1.819160 -0.1808404  0.088212  1.088212 -0.911788

EDIT:Please be aware of the huge memory consumption and low speed: https://ys-l.github.io/posts/2015/08/28/how-not-to-use-pandas-apply/ !


In 2020, I use apply() with argument result_type='expand'

>>> appiled_df = df.apply(lambda row: fn(row.text), axis='columns', result_type='expand')>>> df = pd.concat([df, appiled_df], axis='columns')


matomo