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:
import pandas as pd
to_drop
: list of columns to drop in dataframe
# list of columns to drop in dataframe
to_drop = ["team", "points", "blocks"]
# 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
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
df1 = drop_columns(df, to_drop)
df1