2016-03-26 243 views
1

我試圖select下面的數據使用pandas爲Python 2.7從網頁(http://owww.met.hu/eghajlat/eghajlati_adatsorok/bp/Navig/202_EN.htm)開始從1991年到2000年。有人請幫助我如何編寫代碼。謝謝!從特定年份的熊貓選擇

datum m_ta m_tax  m_taxd m_tan  m_tand 
------- ----- ----- ---------- ----- ---------- 
1901-01 -4.7 5.0 1901-01-23 -12.2 1901-01-10 
1901-02 -2.1 3.5 1901-02-06 -7.9 1901-02-15 
1901-03 5.8 13.5 1901-03-20 0.6 1901-03-01 
1901-04 11.6 18.2 1901-04-10 7.4 1901-04-23 
1901-05 16.8 22.5 1901-05-31 12.2 1901-05-05 
1901-06 21.0 24.8 1901-06-03 14.6 1901-06-17 
1901-07 22.4 27.4 1901-07-30 16.9 1901-07-04 
1901-08 20.7 25.9 1901-08-01 14.7 1901-08-29 
1901-09 15.9 19.9 1901-09-01 11.8 1901-09-09 
1901-10 12.6 17.9 1901-10-04 8.3 1901-10-31 
1901-11 4.7 11.1 1901-11-14 -0.2 1901-11-26 
1901-12 4.2 8.4 1901-12-22 -1.4 1901-12-07 
1902-01 3.4 7.5 1902-01-25 -2.2 1902-01-15 
1902-02 2.8 6.6 1902-02-09 -2.8 1902-02-06 
1902-03 5.3 13.3 1902-03-22 -3.5 1902-03-13 
1902-04 10.5 15.8 1902-04-21 6.1 1902-04-08 
1902-05 12.5 20.6 1902-05-31 8.5 1902-05-10 
1902-06 18.5 23.8 1902-06-30 14.4 1902-06-19 
.... 
+0

所期望的輸出? – jezrael

+0

或者您需要從'1901'到'2000'選擇數據? – jezrael

+0

是的,我需要選擇從1991年到2000年的全部數據。 – Userabc

回答

0

您可以通過datum列使用df.yearboolean indexing選擇數據:

#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 
+0

它現在的作品。謝謝! – Userabc