Skip to main content

Posts

Showing posts with the label drop()

Pandas Drop: Removing Columns from DataFrames

Pandas is a powerful Python library for data manipulation and analysis. One of its most commonly used functions is drop(), which allows you to remove columns from a DataFrame. This can be useful for a variety of reasons, such as: Removing unnecessary or irrelevant columns Cleaning data by removing duplicate or erroneous columns Preparing data for specific tasks or models How to Use Pandas Drop The drop() function takes a list of column names as its first argument. The columns will be removed from the DataFrame and returned as a new DataFrame. The original DataFrame will not be modified. The following example shows how to use the drop() function to remove a single column from a DataFrame: import pandas as pd # Create a DataFrame df = pd.DataFrame({'name': ['Alice', 'Bob', 'Charlie'], 'age': [20, 30, 40], 'city': ['New York', 'Boston', 'Chicago']}) # Remove the 'city' column df = df.drop('city', axi...

Topics

Show more