从特定年份开始的大熊猫选择

时间:2016-03-26 20:14:13

标签: python-2.7 pandas

我试图从1991年到2000年从网页(http://owww.met.hu/eghajlat/eghajlati_adatsorok/bp/Navig/202_EN.htm)使用filter()用于Python 2.7的select以下数据。有人请帮助我。可以写代码。谢谢!

pandas

1 个答案:

答案 0 :(得分:1)

您可以使用df.yearboolean indexing一起按datum列选择数据:

#convert column datum to period
df['datum'] = pd.to_datetime(df['datum']).dt.to_period('M')
#convert columns to datetime
df['m_taxd'] = pd.to_datetime(df['m_taxd'])
df['m_tand'] = pd.to_datetime(df['m_tand'])
print df.datum.dt.year
0     1901
1     1901
2     1901
3     1901
4     1901
5     1901
6     1901
7     1901
8     1901
9     1901
10    1901
11    1901
12    1902
13    1902
14    1902
15    1902
16    1902
17    1902
Name: datum, dtype: int64
#change 1901 to 2000
print df[df.datum.dt.year <= 1901]
     datum  m_ta  m_tax     m_taxd  m_tan     m_tand
0  1901-01  -4.7    5.0 1901-01-23  -12.2 1901-01-10
1  1901-02  -2.1    3.5 1901-02-06   -7.9 1901-02-15
2  1901-03   5.8   13.5 1901-03-20    0.6 1901-03-01
3  1901-04  11.6   18.2 1901-04-10    7.4 1901-04-23
4  1901-05  16.8   22.5 1901-05-31   12.2 1901-05-05
5  1901-06  21.0   24.8 1901-06-03   14.6 1901-06-17
6  1901-07  22.4   27.4 1901-07-30   16.9 1901-07-04
7  1901-08  20.7   25.9 1901-08-01   14.7 1901-08-29
8  1901-09  15.9   19.9 1901-09-01   11.8 1901-09-09
9  1901-10  12.6   17.9 1901-10-04    8.3 1901-10-31
10 1901-11   4.7   11.1 1901-11-14   -0.2 1901-11-26
11 1901-12   4.2    8.4 1901-12-22   -1.4 1901-12-07