Dropping

Dropping rows and columns

To drop rows and columns in a DataFrame, we can use the drop() method.

For example, to drop the first row from the election DataFrame, we can use the following code:

elections.head()
Year Candidate Party Popular vote Result %
0 1824 Andrew Jackson Democratic-Republican 151271 loss 57.210122
1 1824 John Quincy Adams Democratic-Republican 113142 win 42.789878
2 1828 Andrew Jackson Democratic 642806 win 56.203927
3 1828 John Quincy Adams National Republican 500897 loss 43.796073
4 1832 Andrew Jackson Democratic 702735 win 54.574789
elections.drop(columns=['Popular vote'])
Year Candidate Party Result %
0 1824 Andrew Jackson Democratic-Republican loss 57.210122
1 1824 John Quincy Adams Democratic-Republican win 42.789878
2 1828 Andrew Jackson Democratic win 56.203927
3 1828 John Quincy Adams National Republican loss 43.796073
4 1832 Andrew Jackson Democratic win 54.574789
... ... ... ... ... ...
177 2016 Jill Stein Green loss 1.073699
178 2020 Joseph Biden Democratic win 51.311515
179 2020 Donald Trump Republican loss 46.858542
180 2020 Jo Jorgensen Libertarian loss 1.177979
181 2020 Howard Hawkins Green loss 0.255731

182 rows × 5 columns

# Drop the first row
elections.drop(index=0)

# Drop the first two rows
elections.drop(index=[0, 1])
Year Candidate Party Popular vote Result %
2 1828 Andrew Jackson Democratic 642806 win 56.203927
3 1828 John Quincy Adams National Republican 500897 loss 43.796073
4 1832 Andrew Jackson Democratic 702735 win 54.574789
5 1832 Henry Clay National Republican 484205 loss 37.603628
6 1832 William Wirt Anti-Masonic 100715 loss 7.821583
... ... ... ... ... ... ...
177 2016 Jill Stein Green 1457226 loss 1.073699
178 2020 Joseph Biden Democratic 81268924 win 51.311515
179 2020 Donald Trump Republican 74216154 loss 46.858542
180 2020 Jo Jorgensen Libertarian 1865724 loss 1.177979
181 2020 Howard Hawkins Green 405035 loss 0.255731

180 rows × 6 columns