Skip to content

Column Assignment Methods

CAM-1: Direct assignment

df["c"] = [7, 8, 9]
The most common method for assigning values to a column. If the column doesn't exist, it creates a new one.

Supported

CAM-2: Attribute assignment

df.col = value
Works only for valid Python identifiers. Not recommended for production code due to potential conflicts with DataFrame methods.

Not Supported

CAM-3: loc indexer

df.loc[:, 'col'] = value
Label-based column assignment that can assign to slices or selections of rows.

Not Supported

CAM-4: iloc indexer

df.iloc[:, index] = value
Position-based assignment that uses integer indices instead of column labels.

Not Supported

CAM-5: at indexer

df.at[row, 'col'] = value
For single cell assignment based on labels. Faster than loc for single-cell operations.

Not Supported

CAM-6: iat indexer

df.iat[row, col_idx] = value
Single cell assignment by position. Faster than iloc for single-cell operations.

Not Supported

CAM-7: assign method

df = df.assign(A=[1, 2, 3])
Returns a new DataFrame with the column added or modified. Great for method chaining.

Tested but not supported

CAM-8: Multiple assign

df = df.assign(B=1, C=2)
Creates multiple columns at once using keyword arguments.

Not Supported

CAM-9: insert method

df.insert(0, "A", [1, 2, 3])
Inserts a column at a specific position in the DataFrame. Modifies in place.

Supported

CAM-10: setitem with list

df[["c", "d"]] = [[7, 8, 9], [10, 11, 12]]
Assigns multiple columns at once, either from other columns or external values.

Supported

CAM-11: From dictionary

df = pd.DataFrame({'A': [1,2], 'B': [3,4]})
Creates a DataFrame with the specified columns directly in the constructor.

Not Supported

CAM-12: concat

df = pd.concat([df, new_df], axis=1)
Combines columns from two DataFrames through horizontal concatenation.

Not Supported

CAM-13: join

df = df.join(other_df)
Adds columns from another DataFrame based on index alignment.

Not Supported

CAM-14: merge

df = df.merge(df2, on='key')
Adds columns through merging DataFrames on common columns or indices.

Not Supported

CAM-15: From eval

df.eval('C = A * 2', inplace=True)
Creates columns using string expressions evaluated against the DataFrame.

Not Supported

CAM-16: From query results

df['new'] = df.query('A > 0')['B']
Assigns values from a filtered subset of another column.

Not Supported

CAM-17: Conditional assignment

df.loc[df['A'] > 0, 'B'] = 1
Updates columns only for rows matching a condition.

Not Supported

CAM-18: Copy from another column

df['B'] = df['A'].copy()
Creates an explicit copy of another column.

Not Supported

CAM-19: Transform operations

df['B'] = df['A'].apply(lambda x: x*2)
Creates columns from transformations applied to existing columns.

Not Supported

CAM-20: Expanding columns

df[['A','B']] = df['combined'].str.split(expand=True)
Creates multiple columns from string operations that expand data.

Not Supported

CAM-21: From groupby

df['new'] = df.groupby('A')['B'].transform('mean')
Creates columns containing aggregated results broadcast back to the original DataFrame's shape.

Not Supported

CAM-22: From pivot

df.pivot(columns='A')
Creates multiple columns through pivot operations that reshape the DataFrame.

Not Supported

CAM-23: From unstack

df = df.unstack()
Converts index levels to columns in a multi-index DataFrame.

Not Supported

CAM-24: From pd.get_dummies

df = pd.get_dummies(df, columns=['A'])
Creates multiple binary indicator columns from categorical columns.

Not Supported

CAM-25: Update method

df.update(other_df[['A']])
Updates existing columns with values from another DataFrame.

Not Supported