Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | Pandas DataFrame.truncate
Next article icon

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages.Pandasis one of those packages which makes importing and analyzing data much easier. In this article, we will how to delete a row in Excel using Pandas as well as delete a column from DataFrame usingPandas.

Pandas DataFrame drop() Method Syntax

Syntax:DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')

Parameters:

  • labels:String or list of strings referring row or column name.
  • axis:int or string value, 0 'index' for Rows and 1 'columns' for Columns.
  • index or columns:Single label or list. index or columns are an alternative to axis and cannot be used together.level:Used to specify level in case data frame is having multiple level index.
  • inplace:Makes changes in original Data Frame if True.
  • errors:Ignores error if any value from the list doesn't exists and drops rest of the values when errors = 'ignore'

Return type: Dataframe with dropped values

Python Drop Function in Pandas

Pandas provide data analysts with a way to delete and filter data frames usingdataframe.drop() the method. Rows or columns can be removed using an index label or column name using this method.

Deleting Rows and Columns from Pandas DataFrame

Below are some ways and example by which we can delete a row in Excel using Pandas inPython.

Dropping Rows in Pandas by Index Label

In this code, A list of index labels is passed and the rows corresponding to those labels are dropped using.drop() method. To download the CSV used in the code, clickhere.

Python3
# importing pandas moduleimportpandasaspd# making data frame from csv filedata=pd.read_csv("nba.csv",index_col="Name")print(data.head(5))

Output:Data Frame before Dropping values

Name              Team  Number Position   Age Height  Weight            College     Salary              Avery Bradley  Boston Celtics     0.0       PG  25.0    6-2   180.0              Texas  7730337.0Jae Crowder     Boston Celtics     99.0       SF  25.0    6-6   235.0          Marquette  6796117.0John Holland    Boston Celtics     30.0       SG  27.0    6-5   205.0  Boston University        NaNR.J. Hunter        Boston Celtics     28.0       SG  22.0    6-5   185.0      Georgia State  1148640.0Jonas Jerebko   Boston Celtics      8.0       PF  29.0   6-10   231.0                NaN  5000000.0

Applying thedropfunction.

Python3
# dropping passed valuesdata.drop(["Avery Bradley","John Holland","R.J. Hunter"],inplace=True)# displayprint(data)

Output: Data Frame after Dropping values

As shown in the output before, the new output doesn't have the passed values. Those values were dropped and the changes were made in the original data frame since inplace was True.

                         Team  Number Position   Age Height  Weight   College     SalaryName                                                                  Jae Crowder    Boston Celtics    99.0       SF  25.0    6-6   235.0 Marquette  6796117.0Jonas Jerebko  Boston Celtics     8.0       PF  29.0   6-10   231.0       NaN  5000000.0 Amir Johnson   Boston Celtics    90.0       PF  29.0    6-9   240.0       NaN 12000000.0 Jordan Mickey  Boston Celtics    55.0       PF  21.0    6-8   235.0       LSU  1170960.0 Kelly Olynyk   Boston Celtics    41.0        C  25.0    7-0   238.0   Gonzaga  2165160.0

Dropping Columns in Pandas with Column Name

In this code, Passed columns are dropped using column names.axisparameter is kept 1 since 1 refers to columns.

Python3
# importing pandas moduleimportpandasaspd# making data frame from csv filedata=pd.read_csv("nba.csv",index_col="Name")print(data.head())

Output:Data Frame before Dropping Columns

                         Team  Number Position   Age Height  Weight            College     SalaryName                                                                  Avery Bradley  Boston Celtics     0.0       PG  25.0    6-2   180.0              Texas  7730337.0Jae Crowder    Boston Celtics    99.0       SF  25.0    6-6   235.0          Marquette  6796117.0John Holland   Boston Celtics    30.0       SG  27.0    6-5   205.0  Boston University        NaNR.J. Hunter    Boston Celtics    28.0       SG  22.0    6-5   185.0      Georgia State  1148640.0Jonas Jerebko  Boston Celtics     8.0       PF  29.0   6-10   231.0                NaN  5000000.0

Applyingdropfunction.

Python3
# dropping passed columnsdata.drop(["Team","Weight"],axis=1,inplace=True)# displayprint(data.head())

Output: Data Frame after Dropping Columns

As shown in the output images, the new output doesn't have the passed columns. Those values were dropped since the axis was set equal to 1 and the changes were made in the original data frame since inplace was True.

               Number Position   Age Height            College     SalaryName                                                                     Avery Bradley     0.0       PG  25.0    6-2              Texas  7730337.0Jae Crowder      99.0       SF  25.0    6-6          Marquette  6796117.0John Holland     30.0       SG  27.0    6-5  Boston University        NaNR.J. Hunter      28.0       SG  22.0    6-5      Georgia State  1148640.0Jonas Jerebko     8.0       PF  29.0   6-10                NaN  5000000.0

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp