janitor.move

janitor.move(df: pandas.core.frame.DataFrame, source: Union[int, str], target: Union[int, str], position: str = 'before', axis: int = 0) → pandas.core.frame.DataFrame[source]

Move column or row to a position adjacent to another column or row in dataframe. Must have unique column names or indices.

This operation does not reset the index of the dataframe. User must explicitly do so.

Does not apply to multilevel dataframes.

Functional usage syntax:

df = move(df, source=3, target=15, position='after', axis=0)

Method chaining syntax:

import pandas as pd
import janitor
df = pd.DataFrame(...).move(source=3, target=15, position='after',
axis=0)
Parameters
  • df – The pandas Dataframe object.

  • source – column or row to move

  • target – column or row to move adjacent to

  • position – Specifies whether the Series is moved to before or after the adjacent Series. Values can be either ‘before’ or ‘after’; defaults to ‘before’.

  • axis – Axis along which the function is applied. 0 to move a row, 1 to move a column.

Returns

The dataframe with the Series moved.

Raises
  • ValueError – if axis is not 0 or 1.

  • ValueError – if position is not before or after.

  • ValueError – if source row or column is not in dataframe.

  • ValueError – if target row or column is not in dataframe.