Pandas从数据框中提取第一年

时间:2018-05-30 10:17:33

标签: python pandas datetime dataframe

我有这个大数据框,我需要第一次有某些资源可用。让我从我的代码中解释一下。

df1 = df[df['Resource_ID'] == 1348]
df1 = df1[['Format', 'Range_Start', 'Number']]
df1["Range_Start"] = df1["Range_Start"].str[:7]
df1 = df1.groupby(['Format', 'Range_Start'], as_index=True).last()
pd.options.display.float_format = '{:,.0f}'.format
df1 = df1.unstack()
df1.columns = df1.columns.droplevel()

df2 = df1[1:4].sum(axis=0)

df2.name = 'sum'
df2 = df1.append(df2)

df3 = df2.T[['entry', 'sum']].copy()
df3.index = pd.to_datetime(df3.index)

现在打印(df3.first('1D'))会提供以下输出:

Format     entry  sum
Range_Start            
2011-07-01      97   72

我现在可以看到Resource_ID 1348首次出现在2011-07-01,如何从这些信息中仅提取年份?

这是我的示例输入csv数据:

Access_Stat_ID,Resource_ID,Range_Start,Range_End,Name,Format,Number,Matched_URL
1,15,"2009-03-01 00:00:00","2009-03-31 23:59:59","Mar 2009","entry",3,""
203,13,"2009-04-01 00:00:00","2009-04-30 23:59:59","Apr 2009","entry",18,""
204,13,"2009-04-01 00:00:00","2009-04-30 23:59:59","Apr 2009","pdf",7,""

1 个答案:

答案 0 :(得分:0)

似乎需要:

first_year = df3.index.year[0]