按年选择行

时间:2017-11-22 07:06:13

标签: python pandas

我希望按年选择行:

import pandas as pd
import numpy as np

df = pd.DataFrame(columns=['Start','End'],data=[[np.datetime64('2001-01-01'),np.datetime64('2001-07-01')],[np.datetime64('2002-01-01'),np.datetime64('2002-11-01')]])
print(df)

       Start        End
0 2001-01-01 2001-07-01
1 2002-01-01 2002-11-01

所以我试试:

df_2001 = df.loc[df['Start'.year == 2001)]]
Traceback (most recent call last):
  Python Shell, prompt 16, line 1
invalid syntax: <string>, line 1, pos 30

如何在datetime64列中按年份选择?

1 个答案:

答案 0 :(得分:1)

使用dt.year

df_2001 = df[df['Start'].dt.year == 2001]