Adding new columns by transforming existing columns

In some cases, you might wish to add a new column that is a function of existing columns. In the following example, the new column, titled example_new_column_3, is added as a sum of the existing columns, old_column_1 and old_column_2. The axis=1 argument indicates that you wish to take the horizontal sum across the columns instead of the vertical sum of columns:

df['new_col3'] = df[[
'col1','col2'
]].sum(axis=1)

print(df)

The output is as follows:

   col1  col2 col3 new_col1  new_col2  new_col3
0     1     4    x                  0         5
1     2     5    y                  0         7
2     3     6    z                  0         9

The following second example accomplishes a similar task using the pandas apply() function. apply() is a special function because it allows you to apply any function to columns in a DataFrame (including your own custom functions):

old_column_list = ['col1','col2']
df['new_col4'] = df[old_column_list].apply(sum, axis=1)
print(df)

The output is as follows:

   col1  col2 col3 new_col1  new_col2  new_col3  new_col4
0     1     4    x                  0         5         5
1     2     5    y                  0         7         7
2     3     6    z                  0         9         9
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset