Connect with us

Hi, what are you looking for?

Latest

How to Drop One or More Columns from the Pandas Dataframe?

How to Drop One or More Columns from the Pandas Dataframe?

If you work with a large panda data frame with multiple columns, you often want to throw one or more columns out of the panda data frame.

How to Drop One or More Columns from the Pandas Dataframe?

How do you drop the columns in pandas?

If the columns are not needed for further analysis, they are usually omitted. With the Pandas drop function, you can drop/remove one or more columns in the data frame.

Let’s look at some examples of removing columns from a real data table. Download pandas and gapminder data from the URL.

1

2

3

4

pd

gapminder = pd.read_csv(gapminder_url)

gapminder.head()

Let’s filter a bit to reduce the size of the data frame, to illustrate the examples of using the drop function in pandas. After filtering, we have a smaller data block, with four rows and six columns.

1

2

3

4

gapminder_ocean = gapminder[(gapminder.year >2000) &

(gapminder.continent=== ‘Oceania’)]

ocean hapminder.form

Reduction of greenhouse gas emissions

How do I reset a single column of the date frame?

To launch a column from the data frame of the panda, we have to list the name of the column to be launched as an argument for the launch function. Here we have a list that contains only one element, the pop variable. With the Panda Drop function, you can drop a column or row. To indicate that we want to launch a column, we must give ax=1 as another argument for the launch function.

1

2

gapminder_ocean.drop([‘pop’], as=1).

The resulting data framework will have only five columns instead of six. The column with the popup variable is now removed.

1

2

3

4

5

    country ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann anned year ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann ann anned

70 Australia 2002 Oceania 80 370 30687 75473

71 Australia 2007 Oceania 81 235 34435 36744

1102 New Zealand 2002 Oceania 79 110 23189 80135

1103 New Zealand 2007 Oceania 80 204 25185 00911

How do I reset multiple columns of the date frame?

The Drop function of pandas can be used to reset different columns. To launch or delete multiple columns, simply enter all the column names you want to launch. Here is an example of three columns falling out of a Gapmader frame

1

2

gapminder_ocean.drop([‘pop’, ‘gdpPercap’, ‘continent’], as=1).

Note that the resulting data framework now contains only three columns instead of six.

1

2

3

4

5

Country Year of Expert

70 Australia 2002 80,370

71 Australia 2007 81,235

1102 New Zealand 2002 79 110

1103 New Zealand 2007 80,204

How do I throw lines out of the diaphragm?

Pandas also make it easy to move rows of data frames. We can use the same reset function to reset the pandas.

To remove one or more lines from the Pandas data frame, we need to specify the line indices of the lines to be removed and the argument as=0. Here, the argument as=0 indicates that we want to skip rows instead of columns.

How to Drop One or More Columns from the Pandas Dataframe?

How do you organize a panda fight?

In this case, the line indexes are numbers, which allows us to make a list of line numbers that are ignored. Here we missed the lines corresponding to Australia.

1

2

3

4

5

gapminder_ocean.drop([70,71],as=0)

Year Country Pop Continental LifeEhr gpPerkap

1102 New Zealand 2002 3908037,0 Oceania 79 110 23189 80135

1103 New Zealand 2007 4115771,0 Oceania 80 204 25185 00911

We can also omit columns or rows with an argument in_place = True.

 

 

 pandas drop multiple columns by index,drop unnamed column pandas

You May Also Like