A handy function to add a column anywhere in a Pandas DataFrame

If you want to add a column anywhere in Pandas DataFrame, use pandas.DataFrame.insert It can be achieved by using it, but there are some complaints.

--Not immutable (DataFrame is directly rewritten / destructive method / no inplace option) --The place to add must be specified by the numerical value of the index

I wrote a convenient function to solve these problems.

Implementation

from typing import Union, Optional
import pandas as pd


def insert_columns(
        df: pd.DataFrame,
        data: Union[pd.Series, pd.DataFrame],
        *,
        before: Optional[str] = None,
        after: Optional[str] = None,
        allow_duplicates: bool = False,
        inplace: bool = False,
    ) -> pd.DataFrame:

    if not inplace:
        df = df.copy()

    if not (after is None) ^ (before is None):
        raise ValueError('Specify only "before" or "after"')

    if before:
        loc = df.columns.get_loc(before)
    else:
        loc = df.columns.get_loc(after) + 1

    if type(data) is pd.Series:
        df.insert(loc, data.name, data, allow_duplicates)
    elif type(data) is pd.DataFrame:
        for column in data.columns[::-1]:
            df.insert(loc, column, data[column], allow_duplicates)

    return df

--Specify the column name for before or ʻafter --By default, the input DataFrame is not directly rewritten. --Rewrite directly by specifying ʻinplace = True

How to use

Use sklearn's iris dataset as sample data.

from sklearn import datasets

iris = datasets.load_iris()

df = pd.DataFrame(iris.data, columns=iris.feature_names)
target = pd.Series(iris.target_names[iris.target], name='target')
df.head()

image.png

target.head()

image.png

Try adding target after sepal width (cm) in df.

insert_columns(df, target, after='sepal width (cm)')

image.png

In this example, Series is added, but DataFrame can also be specified.

Recommended Posts

A handy function to add a column anywhere in a Pandas DataFrame
How to get a specific column name and index name in pandas DataFrame
Function to return multi columns to single column in DataFrame
Put the lists together in pandas to make a DataFrame
[Python] How to add rows and columns to a table (pandas DataFrame)
Attempt to extend a function in the library (add copy function to pathlib)
How to reassign index in pandas dataframe
[Pandas_flavor] Add a method of Pandas DataFrame
[Python] Add total rows to Pandas DataFrame
How to find a specific type (str, float etc) column in a DataFrame column
To execute a Python enumerate function in JavaScript
To add a module to python put in Julialang
Add totals to rows and columns in pandas
How to Mock a Public function in Pytest
Check if the expected column exists in Pandas DataFrame
Convert comma-separated numeric strings to numbers in Pandas DataFrame
How to display DataFrame as a table in Markdown
Covector to think in function
Create a function in Python
Add a dictionary to MeCab
How to call a function
How to import a file anywhere you like in Python
Ingenuity to handle data with Pandas in a memory-saving manner
To return char * in a callback function using ctypes in Python
Let's create a function to hold down Button in Tkinter
Export pandas dataframe to excel
python / pandas / dataframe / How to get the simplest row / column / index / column
I wrote a function to load a Git extension script in Python
How to find the memory address of a Pandas dataframe value
<Pandas> How to handle time series data in a pivot table
I want to make the second line the column name in pandas
Create a function to get the contents of the database in Go
DataFrame of pandas From creating a DataFrame from two lists to writing a file
Function to convert Excel column to number
To add a C module to MicroPython ...
How to write soberly in pandas
Create a pandas Dataframe from a string.
How to make a recursive function
Extract elements by Pandas column name x row number
update dataframe
Replace column names / values with pandas dataframe
How to get a specific column name and index name in pandas DataFrame
[Python] Sort the table by sort_values (pandas DataFrame)
Sort by pandas
Convert pandas dataframe elements to regular string type
Check if the expected column exists in Pandas DataFrame
3D plot Pandas DataFrame
Python application: Pandas # 3: Dataframe
A handy function to add a column anywhere in a Pandas DataFrame
[Python / Tkinter] Search for Pandas DataFrame → Create a simple search form to display
[Python / Pandas] A bug occurs when trying to replace a DataFrame with `None` with` replace`
Added a function to register desired shifts in the Django shift table
How to sort by specifying a column in the Python Numpy array.
Add a GPIO board to your computer. (1)
How to split and save a DataFrame
I want to print in a comprehension
Precautions when pickling a function in python
How to add a package with PyCharm
A simple IDAPython script to name a function
How to get a stacktrace in python
In Jupyter, add IPerl to the kernel.
[Pandas] Expand the character string to DataFrame
[Python] Pandas to fully understand in 10 minutes
Launch a Flask app in Python Anywhere
[V11 ~] A memorandum to put in Misskey
Replace column names / values with pandas dataframe
Create a dataframe from excel using pandas
How to read CSV files in Pandas
Adding Series to columns in python pandas
Download Pandas DataFrame as a CSV file
Add a Python virtual environment to VSCode