Reputation: 21
I've been trying to plot a graph for a specific subset of my data. My data has got the data from the years 1960 to 2018. However, I am only interested in plotting my histogram using only a specific column's variable and the rows that display data from 1981 onwards.
So far I've tried plotting using 2 variables
x = df1y.index
which returns the values:
Int64Index([1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991,
1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013,
2014, 2015, 2016, 2017],
dtype='int64', name=' Time ')
and
y = df1.iloc[21:58, [15]] ## 21 to 58 are rows for 1981 to 2017 while column 15 refers to the column I've been trying to get
y returns:
Resident Live-births(Number)
Time
1981 41,000
1982 41,300
1983 39,300
1984 40,200
1985 41,100
1986 37,159
1987 42,362
1988 51,537
1989 46,361
1990 49,787
1991 47,805
1992 47,907
1993 48,739
1994 48,075
1995 46,916
1996 46,707
1997 45,356
1998 41,636
1999 41,327
2000 44,765
2001 39,281
2002 38,555
2003 35,474
2004 35,135
2005 35,528
2006 36,272
2007 37,074
2008 37,277
2009 36,925
2010 35,129
2011 36,178
2012 38,641
2013 35,681
2014 37,967
2015 37,861
2016 36,875
2017 35,444
After keying in
x = df1y.index
y = df1.iloc[21:58, [15]]
plt.plot(x, y, 'o-')
I've received an error:
TypeError: unhashable type: 'numpy.ndarray'
Upvotes: 1
Views: 3902
Reputation: 10860
use
y = df1.iloc[21:58, 15].values
to do it the way you tried to walk
However, normally you don't want to calculate the subset indices by yourself, so consider sth like
y = df1.loc[df1.index > 1981, 'name_of_your_column_15_here'].values
to get the numpy array of the (y-)values you want to have.
And to get some more convenience, just try and apply .plot()
directly to the series (works also with whole dataframes) and look what happens...
idx_slice = df1.index > 1981
df1.loc[idx_slice, 'name_of_your_column_15_here'].plot()
Upvotes: 1