janitor.add_column

janitor.add_column(df: pandas.core.frame.DataFrame, column_name: str, value: Union[List[Any], Tuple[Any], Any], fill_remaining: bool = False) → pandas.core.frame.DataFrame[source]

Add a column to the dataframe.

This method does not mutate the original DataFrame.

Intended to be the method-chaining alternative to:

df[column_name] = value

Method chaining syntax adding a column with only a single value:

# This will add a column with only one value.
df = pd.DataFrame(...).add_column(column_name="new_column", 2)

Method chaining syntax adding a column with more than one value:

# This will add a column with an iterable of values.
vals = [1, 2, 5, ..., 3, 4]  # of same length as the dataframe.
df = pd.DataFrame(...).add_column(column_name="new_column", vals)
Parameters
  • df – A pandas DataFrame.

  • column_name – Name of the new column. Should be a string, in order for the column name to be compatible with the Feather binary format (this is a useful thing to have).

  • value – Either a single value, or a list/tuple of values.

  • fill_remaining – If value is a tuple or list that is smaller than the number of rows in the DataFrame, repeat the list or tuple (R-style) to the end of the DataFrame.

Returns

A pandas DataFrame with an added column.

Raises
  • ValueError – if attempting to add a column that already exists.

  • ValueError – if value has more elements that number of rows in the DataFrame.

  • ValueError – if attempting to add an iterable of values with a length not equal to the number of DataFrame rows.

  • ValueError – if value has length of 0.