search
Search
Unlock 100+ guides
search toc
close
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

# Removing rows using integer index in Pandas DataFrame

schedule Aug 11, 2023
Last updated
local_offer
PythonPandas
Tags
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

# Solution

To remove rows using integer index in Pandas DataFrame:

1. get the name of the row index using `iloc`.

2. use the `drop(~)` method to remove the row.

# Removing a single row using integer index

Consider the following DataFrame:

``` df = pd.DataFrame({"A":[2,3,4],"B":[5,6,7]}, index=["a","b","c"])df A Ba 2 5b 3 6c 4 7 ```

To remove row at integer index `1`:

``` df.drop(index=df.iloc[1].name) A Ba 2 5c 4 7 ```

## Explanation

The `drop(~)` method is used to remove rows and columns, but the caveat is that you can only remove rows/column by their name. Therefore, we first use `iloc` to extract the row at integer index `1`, and then extract its name using the `name` property:

``` df.iloc[1].name 'b' ```
NOTE

The `drop(~)` method returns a new DataFrame, that is, the original `df` is kept intact. To directly modify the original `df`, set `inplace=True`.

# Removing multiple rows using integer index

Consider the same `df` as above:

``` df = pd.DataFrame({"A":[2,3,4],"B":[5,6,7]}, index=["a","b","c"])df A Ba 2 5b 3 6c 4 7 ```

To remove multiple rows at integer index `0` and `1`:

``` df.drop(index=df.iloc[[0,1]].index) A Bc 4 7 ```

## Explanation

We first extract rows at integer index `0` and `1` as a DataFrame using `iloc`:

``` df.iloc[[0,1]] A Ba 2 5b 3 6 ```

We then fetch the index of this DataFrame using the `index` property:

``` df.iloc[[0,1]].index Index(['a', 'b'], dtype='object') ```

Now that we have the labels of the rows that we want to remove, directly pass this into `drop(~)`:

``` df.drop(index=df.iloc[[0,1]].index) A Bc 4 7 ```
Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!