How to drop multiple columns in pandas

Share This Post

Sometimes you may want to set a data name by saving or minimizing other columns while using Pandas data.

We can discard columns in a few ways. We will use the Pandas drop() function to learn to drop more columns and get a minor Pandas data name.

For our toy data, we also implied NumPy data. Using a random NumPy module with index names and columns, we create data for Pandas data frame.

Place Multiple Columns using Pandas drop() with axis = 1

We can manage the Pandas drop() function from the data frame to drop multiple columns. Pandas drop() is flexible and can use data lists to drop as well.

To use the Pandas drop() function to drop columns, several columns that need to be dropped as a list are also provided by Pandas. In addition, to tell the drop() function.

We also need to designate the axis = 1 argument that we are dropping columns. The process drops lines of the data list when the axis = 0 drops ().

Place More Columns using Pandas drop() in columns

Without using the axis = 1 argument, we can also use the Pandas drop() function. However, we need to designate the “columns” to discuss the list of column names to be eliminated.

For example, we need to designate “columns = [‘A’, ‘B’]” as a function of the drop() function to discard columns A and B to see similar results as before, and the two columns will drop.

To dump many local columns, we can also use the Pandas drop() function. This changes the actual name of the data. We are dropping columns without creating a new data name.” in place = True” is designated.

Another way to lower specific columns is to select the remaining columns using the Pandas [[]] function. For example, by discarding columns A and B, in this instance, C and D, we can choose the remaining columns, And the same results are shown.

# Import pandas package


import pandas as pd

# create a dictionary with five fields each


data = {

'A':['A1', 'A2', 'A3', 'A4', 'A5'],

'B':['B1', 'B2', 'B3', 'B4', 'B5'],

'C':['C1', 'C2', 'C3', 'C4', 'C5'],

'D':['D1', 'D2', 'D3', 'D4', 'D5'],

'E':['E1', 'E2', 'E3', 'E4', 'E5']
}

# Convert the dictionary into DataFrame

df = pd.dataFrame(data)

Read more: How to find IP address using Python and cmd Windows 10?

spot_img

Related Posts

Python for Loop Iteration: How to Easily Manipulate a List

One of the most powerful features of the Python...

How To Get First Characters of a String in Python

Way to find the first characters of a string...

How To Convert Tuple To String In Python

In this post, you will learn how to convert...

How To Convert String to Double in Python

Good to hear, that you want to convert a...

Python TypeError: String Index Out Of Range Solution

A string is an array of characters and part...

How To Remove Empty Strings From a List Of Strings

On this page, we will discuss how to remove...
- Advertisement -spot_img