Pandas中DataFrame数据查询

import quandl

# Data from http://www.quandl.com/SIDC/SUNSPOTS_ASunspot-Numbers-Annual
# PyPi https://pypi.python.org/pypi/Quandl
sunspots = quandl.get('SIDC/SUNSPOTS_A')
# 读取前面三个数据记录head(),读取后面四个记录tail()
# 将数据保存为execl格式,但是日期格式出现问题了。
# sunspots.to_excel('E:/Python-code/DataBasic/chapter3Pandas/Sunspots.xlsx')
# 查询最近日期的太阳黑子数据,iloc依据索引查询数据
last_date = sunspots.index[-1]
print('Last value', sunspots.loc[last_date], last_date)
# 通过YYYYMMDD格式查询数据
print('Values slice by date:\n', sunspots['20020101':'20130101'])
# 使用.iloc索引查询数据
print('Slice from a list of indices:\n', sunspots.iloc[[1, 2, -2, -1]])
# 使用boolean型数据
print('Boolean selection', sunspots[sunspots > sunspots.mean()])
print('Boolean selection with column label :\n', sunspots[sunspots['Number of Observations'] >
                                                          sunspots['Number of Observations'].mean()])
Last value Yearly Mean Total Sunspot Number        7.0
Yearly Mean Standard Deviation          1.1
Number of Observations              12250.0
Definitive/Provisional Indicator        0.0
Name: 2018-12-31 00:00:00, dtype: float64 2018-12-31 00:00:00
Values slice by date:
             Yearly Mean Total Sunspot Number                ...                 Definitive/Provisional Indicator
Date                                                        ...                                                 
2002-12-31                             163.6                ...                                              1.0
2003-12-31                              99.3                ...                                              1.0
2004-12-31                              65.3                ...                                              1.0
2005-12-31                              45.8                ...                                              1.0
2006-12-31                              24.7                ...                                              1.0
2007-12-31                              12.6                ...                                              1.0
2008-12-31                               4.2                ...                                              1.0
2009-12-31                               4.8                ...                                              1.0
2010-12-31                              24.9                ...                                              1.0
2011-12-31                              80.8                ...                                              1.0
2012-12-31                              84.5                ...                                              1.0

[11 rows x 4 columns]
Slice from a list of indices:
             Yearly Mean Total Sunspot Number                ...                 Definitive/Provisional Indicator
Date                                                        ...                                                 
1701-12-31                              18.3                ...                                              1.0
1702-12-31                              26.7                ...                                              1.0
2017-12-31                              21.7                ...                                              1.0
2018-12-31                               7.0                ...                                              0.0

[4 rows x 4 columns]
Boolean selection             Yearly Mean Total Sunspot Number                ...                 Definitive/Provisional Indicator
Date                                                        ...                                                 
1700-12-31                               NaN                ...                                              1.0
1701-12-31                               NaN                ...                                              1.0
1702-12-31                               NaN                ...                                              1.0
1703-12-31                               NaN                ...                                              1.0
1704-12-31                               NaN                ...                                              1.0
1705-12-31                              96.7                ...                                              1.0
1706-12-31                               NaN                ...                                              1.0
1707-12-31                               NaN                ...                                              1.0
1708-12-31                               NaN                ...                                              1.0
1709-12-31                               NaN                ...                                              1.0
1710-12-31                               NaN                ...                                              1.0
1711-12-31                               NaN                ...                                              1.0
1712-12-31                               NaN                ...                                              1.0
1713-12-31                               NaN                ...                                              1.0
1714-12-31                               NaN                ...                                              1.0
1715-12-31                               NaN                ...                                              1.0
1716-12-31                               NaN                ...                                              1.0
1717-12-31                             105.0                ...                                              1.0
1718-12-31                             100.0                ...                                              1.0
1719-12-31                               NaN                ...                                              1.0
1720-12-31                               NaN                ...                                              1.0
1721-12-31                               NaN                ...                                              1.0
1722-12-31                               NaN                ...                                              1.0
1723-12-31                               NaN                ...                                              1.0
1724-12-31                               NaN                ...                                              1.0
1725-12-31                               NaN                ...                                              1.0
1726-12-31                             130.0                ...                                              1.0
1727-12-31                             203.3                ...                                              1.0
1728-12-31                             171.7                ...                                              1.0
1729-12-31                             121.7                ...                                              1.0
...                                      ...                ...                                              ...
1989-12-31                             211.1                ...                                              1.0
1990-12-31                             191.8                ...                                              1.0
1991-12-31                             203.3                ...                                              1.0
1992-12-31                             133.0                ...                                              1.0
1993-12-31                               NaN                ...                                              1.0
1994-12-31                               NaN                ...                                              1.0
1995-12-31                               NaN                ...                                              1.0
1996-12-31                               NaN                ...                                              1.0
1997-12-31                               NaN                ...                                              1.0
1998-12-31                              88.3                ...                                              1.0
1999-12-31                             136.3                ...                                              1.0
2000-12-31                             173.9                ...                                              1.0
2001-12-31                             170.4                ...                                              1.0
2002-12-31                             163.6                ...                                              1.0
2003-12-31                              99.3                ...                                              1.0
2004-12-31                               NaN                ...                                              1.0
2005-12-31                               NaN                ...                                              1.0
2006-12-31                               NaN                ...                                              1.0
2007-12-31                               NaN                ...                                              1.0
2008-12-31                               NaN                ...                                              1.0
2009-12-31                               NaN                ...                                              1.0
2010-12-31                               NaN                ...                                              1.0
2011-12-31                              80.8                ...                                              1.0
2012-12-31                              84.5                ...                                              1.0
2013-12-31                              94.0                ...                                              1.0
2014-12-31                             113.3                ...                                              1.0
2015-12-31                               NaN                ...                                              1.0
2016-12-31                               NaN                ...                                              1.0
2017-12-31                               NaN                ...                                              1.0
2018-12-31                               NaN                ...                                              NaN

[319 rows x 4 columns]
Boolean selection with column label :
             Yearly Mean Total Sunspot Number                ...                 Definitive/Provisional Indicator
Date                                                        ...                                                 
1981-12-31                             198.9                ...                                              1.0
1982-12-31                             162.4                ...                                              1.0
1983-12-31                              91.0                ...                                              1.0
1984-12-31                              60.5                ...                                              1.0
1985-12-31                              20.6                ...                                              1.0
1986-12-31                              14.8                ...                                              1.0
1987-12-31                              33.9                ...                                              1.0
1988-12-31                             123.0                ...                                              1.0
1989-12-31                             211.1                ...                                              1.0
1990-12-31                             191.8                ...                                              1.0
1991-12-31                             203.3                ...                                              1.0
1992-12-31                             133.0                ...                                              1.0
1993-12-31                              76.1                ...                                              1.0
1994-12-31                              44.9                ...                                              1.0
1995-12-31                              25.1                ...                                              1.0
1996-12-31                              11.6                ...                                              1.0
1997-12-31                              28.9                ...                                              1.0
1998-12-31                              88.3                ...                                              1.0
1999-12-31                             136.3                ...                                              1.0
2000-12-31                             173.9                ...                                              1.0
2001-12-31                             170.4                ...                                              1.0
2002-12-31                             163.6                ...                                              1.0
2003-12-31                              99.3                ...                                              1.0
2004-12-31                              65.3                ...                                              1.0
2005-12-31                              45.8                ...                                              1.0
2006-12-31                              24.7                ...                                              1.0
2007-12-31                              12.6                ...                                              1.0
2008-12-31                               4.2                ...                                              1.0
2009-12-31                               4.8                ...                                              1.0
2010-12-31                              24.9                ...                                              1.0
2011-12-31                              80.8                ...                                              1.0
2012-12-31                              84.5                ...                                              1.0
2013-12-31                              94.0                ...                                              1.0
2014-12-31                             113.3                ...                                              1.0
2015-12-31                              69.8                ...                                              1.0
2016-12-31                              39.8                ...                                              1.0
2017-12-31                              21.7                ...                                              1.0
2018-12-31                               7.0                ...                                              0.0

[38 rows x 4 columns]

Process finished with exit code 0

你可能感兴趣的:(DataFrame属性)