Pandas: adding several columns to a dataframe in a single line

Viewed 297

I come from R background & am wondering if there's a single line code to add several new columns to an existing dataframe in Pandas just like dplyr. If have this code:

import pandas as pd

df = pd.DataFrame({'a': range(1, 11)})

df['b'] = range(11, 21)
df['c'] = range(21, 31)
df['d'] = range(31, 40)
df['e'] = range(41, 50)

Is there a way to make all columns addition into df in one line? An example of what I want in R would be:

library(dplyr)

df <- data.frame('a' = 1:10)

df <- df %>% mutate(b = 11:20, c = 21:30, d = 31:40, e = 41:50)
5 Answers

There is assign:

df.assign(b=range(11,21), c=range(21,31), d=range(31,41))

Things are even easier when you have a dictionary:

# assume you get this from somewhere else
val_dict = {'b': range(11,21), 'c':range(21,31)}

df.assign(**val_dict)

Note the second approach is expected when b is not a possible choice for keyword arguments, for example, having spaces 'a b'.

As others have noted, you could build them all in the original construction of the dataframe, but if you needed to add multiple columns at a later point, you can add each through multiple declaration:

df['b'], df['c'], df['d'], df['e'] = range(11, 21), range(21,31), range(31,41), range(41,51)
df = pd.DataFrame( {c: range(x, y) for c,x,y in [(chr(97+x), x*10+1, x*10+11) for x in range(5)]})

>>> df
    a   b   c   d   e
0   1  11  21  31  41
1   2  12  22  32  42
2   3  13  23  33  43
3   4  14  24  34  44
4   5  15  25  35  45
5   6  16  26  36  46
6   7  17  27  37  47
7   8  18  28  38  48
8   9  19  29  39  49
9  10  20  30  40  50

Or to add to an existing dataframe:

df = pd.DataFrame({'a': range(1,11)})
df = pd.concat([df, pd.DataFrame( {c: range(x, y) for c,x,y in [(chr(97+x), x*10+1, x*10+11) for x in range(1, 5)]})], axis=1)

Check out this:

>>> from datar.all import f, tibble, mutate
>>> df = tibble(a = f[1:10])
>>> df >> mutate(b = f[11:20], c = f[21:30], d = f[31:40], e = f[41:50])
        a       b       c       d       e
  <int64> <int64> <int64> <int64> <int64>
0       1      11      21      31      41
1       2      12      22      32      42
2       3      13      23      33      43
3       4      14      24      34      44
4       5      15      25      35      45
5       6      16      26      36      46
6       7      17      27      37      47
7       8      18      28      38      48
8       9      19      29      39      49
9      10      20      30      40      50

I am the author of the datar package.

You just pass all the data and associated column name into pd.DataFrame just as you did with column 'a', separate with commas.

Like this:

df = pd.DataFrame({'a': range(1, 11), 'b' : range(11, 21)})
Related