Thursday, February 21, 2019

How do I remove columns from a pandas DataFrame?

pandas3
In [31]:
import pandas as pd
In [32]:
ufo = pd.read_csv('http://bit.ly/uforeports')
In [33]:
ufo.head()
Out[33]:
City Colors Reported Shape Reported State Time
0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00
1 Willingboro NaN OTHER NJ 6/30/1930 20:00
2 Holyoke NaN OVAL CO 2/15/1931 14:00
3 Abilene NaN DISK KS 6/1/1931 13:00
4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00
In [34]:
ufo.shape
Out[34]:
(18241, 5)
In [35]:
ufo.drop('Colors Reported', axis=1, inplace=True)
In [36]:
ufo.head()
Out[36]:
City Shape Reported State Time
0 Ithaca TRIANGLE NY 6/1/1930 22:00
1 Willingboro OTHER NJ 6/30/1930 20:00
2 Holyoke OVAL CO 2/15/1931 14:00
3 Abilene DISK KS 6/1/1931 13:00
4 New York Worlds Fair LIGHT NY 4/18/1933 19:00
In [37]:
ufo.drop(['City', 'State'], axis=1, inplace=True)
In [38]:
ufo.head()
Out[38]:
Shape Reported Time
0 TRIANGLE 6/1/1930 22:00
1 OTHER 6/30/1930 20:00
2 OVAL 2/15/1931 14:00
3 DISK 6/1/1931 13:00
4 LIGHT 4/18/1933 19:00
In [40]:
ufo.drop([0,1] , axis=0, inplace=True)
In [41]:
ufo.head()
Out[41]:
Shape Reported Time
2 OVAL 2/15/1931 14:00
3 DISK 6/1/1931 13:00
4 LIGHT 4/18/1933 19:00
5 DISK 9/15/1934 15:30
6 CIRCLE 6/15/1935 0:00
In [42]:
ufo.shape
Out[42]:
(18239, 2)
In [ ]: