python pandas dataframe drop column

preview_player
Показать описание
In this tutorial, we will explore how to drop a column from a Pandas DataFrame in Python. Pandas is a powerful data manipulation library that provides data structures like DataFrames, which are useful for working with structured data.
Make sure you have Pandas installed. If not, you can install it using the following command:
Let's create a sample DataFrame for demonstration purposes.
To drop a column from a DataFrame, you can use the drop method. Specify the column name and set the axis parameter to 1, indicating that you want to drop a column (as opposed to a row).
In the above example, we drop the 'City' column from the original DataFrame and assign the result to a new DataFrame (df_dropped). The original DataFrame remains unchanged.
You can also drop multiple columns by passing a list of column names to the drop method.
In this example, we drop both the 'Age' and 'City' columns simultaneously.
By default, the drop method returns a new DataFrame with the specified columns removed, leaving the original DataFrame unchanged. If you want to modify the original DataFrame in-place, you can use the inplace parameter.
Using inplace=True, the original DataFrame (df) is modified, and the 'Age' column is dropped.
That's it! You've successfully learned how to drop a column from a Pandas DataFrame in Python. This skill is useful for data manipulation tasks where you need to remove unnecessary information from your dataset.
ChatGPT
Рекомендации по теме