Skip to content

Latest commit

 

History

History
71 lines (49 loc) · 2.16 KB

Pandas_Drop_columns.md

File metadata and controls

71 lines (49 loc) · 2.16 KB



Template request | Bug report | Generate Data Product

Tags: #pandas #snippet #datacleaning #operations

Author: Florent Ravenel

Description: This notebook shows how to define a new DataFrame that drops columns defined in Input section.

References:

Input

Import libraries

import pandas as pd

Setup Variables

  • to_drop: list of columns to drop in dataframe
# list of columns to drop in dataframe
to_drop = ["team", "points", "blocks"]

Model

Create DataFrame

# create DataFrame
df = pd.DataFrame(
    {
        "team": ["A", "A", "A", "B", "B", "B"],
        "points": [11, 7, 8, 10, 13, 13],
        "assists": [5, 7, 7, 9, 12, 9],
        "rebounds": [11, 8, 10, 6, 6, 5],
    }
)
df

Create new DataFrame that drops defined columns

Only columns that exist in DataFrame will be droped with the function below.

def drop_columns(df, to_drop):
    # Check if all columns exist in dataframe
    for c in to_drop:
        if c in df.columns:
            df[c] = df.drop(c, axis=1)
    else:
        print(f"🚨 Columns '{c}' does not exist in DataFrame!")
    return df

Output

Display new DataFrame

df1 = drop_columns(df, to_drop)
df1