Dropping Rows

1. Dropping a Row with specified index
2. Dropping Multiple Rows with specified indexes
3. Drop A Range of Rows with specified range
In [60]:
import pandas as pd

df = pd.read_csv('amazon_fires.csv', encoding = "ISO-8859-1")

new_column_name = { 'ano' : 'year' ,  'mes': 'month', 'estado': 'state',
                   'numero': 'fire_numbers','encontro': 'date'}
                                                
df.rename(columns= new_column_name , inplace=True)   

Month_in_English = {'Janeiro': 'January',
'Fevereiro': 'February',
'Março': 'March',
'Abril': 'April',
'Maio': 'May',
'Junho': 'June',
'Julho': 'July',
'Agosto': 'August',
'Setembro': 'September',
'Outubro': 'October',
'Novembro': 'November',
'Dezembro': 'December'}

df["month"] = df["month"].map(Month_in_English)

new_column_order = [4,1,0,2,3,]

df = df[df.columns[new_column_order]]

df['fire_numbers'] = df['fire_numbers'].str.strip(" Fires")

df['fire_numbers'] = df['fire_numbers'].astype(float)

df = df.dropna() 

df.head()
Out[60]:
date month year state fire_numbers
0 1/1/1998 January 1998 Acre 0.0
1 1/1/1999 January 1999 Acre 0.0
2 1/1/2000 January 2000 Acre 0.0
3 1/1/2001 January 2001 Acre 0.0
4 1/1/2002 January 2002 Acre 0.0

1. Dropping a Row with specified index

In [51]:
df = df.drop(df.index[1])

df.head()
Out[51]:
date month year state fire_numbers
0 1/1/1998 January 1998 Acre 0.0
2 1/1/2000 January 2000 Acre 0.0
3 1/1/2001 January 2001 Acre 0.0
4 1/1/2002 January 2002 Acre 0.0
5 1/1/2003 January 2003 Acre 10.0

Result: Row with index 1 removed

Reset Indexes

- We can use the drop parameter to avoid the old index being added as a column
In [40]:
df = df.reset_index()

df.head()
Out[40]:
index date month year state fire_numbers
0 0 1/1/1998 January 1998 Acre 0.0
1 2 1/1/2000 January 2000 Acre 0.0
2 3 1/1/2001 January 2001 Acre 0.0
3 4 1/1/2002 January 2002 Acre 0.0
4 5 1/1/2003 January 2003 Acre 10.0

Reset Indexes Using Drop Parameter

In [52]:
df = df.reset_index(drop=True)

df.head()
Out[52]:
date month year state fire_numbers
0 1/1/1998 January 1998 Acre 0.0
1 1/1/2000 January 2000 Acre 0.0
2 1/1/2001 January 2001 Acre 0.0
3 1/1/2002 January 2002 Acre 0.0
4 1/1/2003 January 2003 Acre 10.0

2. Dropping Multiple Rows with specified indexes

In [54]:
df = df.drop(df.index[[2,3]])

df = df.reset_index(drop=True)

df.head()
Out[54]:
date month year state fire_numbers
0 1/1/1998 January 1998 Acre 0.0
1 1/1/1999 January 1999 Acre 0.0
2 1/1/2002 January 2002 Acre 0.0
3 1/1/2003 January 2003 Acre 10.0
4 1/1/2004 January 2004 Acre 0.0

Result: Row with index 2 & 3 removed

3. Drop A Range of Rows with specified range

In [59]:
df = df.drop(df.index[[1,4]])

df.head()
Out[59]:
date month year state fire_numbers
0 1/1/1998 January 1998 Acre 0.0
3 1/1/2001 January 2001 Acre 0.0
5 1/1/2003 January 2003 Acre 10.0
7 1/1/2005 January 2005 Acre 12.0
8 1/1/2006 January 2006 Acre 4.0

Result: Row with index 1, 2 , 3 & 4 removed

Reset Indexes Using Drop Parameter

In [61]:
df = df.drop(df.index[[1,4]])

df = df.reset_index(drop=True)

df.head()
Out[61]:
date month year state fire_numbers
0 1/1/1998 January 1998 Acre 0.0
1 1/1/2000 January 2000 Acre 0.0
2 1/1/2001 January 2001 Acre 0.0
3 1/1/2003 January 2003 Acre 10.0
4 1/1/2004 January 2004 Acre 0.0