熊猫-从索引中提取月份和年份

时间:2019-01-09 12:55:54

标签: python pandas datetime

我有一个以日期时间为索引的数据框。如何从索引中提取年份和月份?下面是我的数据框。

            1. open   2. high    3. low  4. close   5. volume
date                                                         
2019-01-07   101.64  103.2681  100.9800    102.06  35656136.0
2019-01-08   103.04  103.9700  101.7134    102.80  31294058.0

显然df["index"].dt.monthdf["date"].dt.month无效。

3 个答案:

答案 0 :(得分:1)

使用DatetimeIndex.yearDatetimeIndex.monthdt用于选择列:

print (df.index)
            1. open   2. high    3. low  4. close   5. volume
date                                                         
2019-01-07   101.64  103.2681  100.9800    102.06  35656136.0
2019-01-08   103.04  103.9700  101.7134    102.80  31294058.0


df.index = pd.to_datetime(df.index)

y = df.index.year
m = df.index.month

print (y)
Int64Index([2019, 2019], dtype='int64', name='date')

print (m)
Int64Index([1, 1], dtype='int64', name='date')

答案 1 :(得分:1)

您可以使用以下示例,但是可以从Docs pandas.DatetimeIndex

获取详细信息

示例DataFrame:

>>> df
                              name  age favorite_color  grade  birth_date
Willard Morris      Willard Morris   20           blue     88  01-02-1996
Al Jennings            Al Jennings   19            red     92  08-05-1997
Omar Mullins          Omar Mullins   22         yellow     95  04-28-1996
Spencer McDaniel  Spencer McDaniel   21          green     70  12-16-1995

1)要提取年份:

>>> df['year'] = pd.DatetimeIndex(df['birth_date']).year
>>> df.head()
                              name  age favorite_color  grade  birth_date  year
Willard Morris      Willard Morris   20           blue     88  01-02-1996  1996
Al Jennings            Al Jennings   19            red     92  08-05-1997  1997
Omar Mullins          Omar Mullins   22         yellow     95  04-28-1996  1996
Spencer McDaniel  Spencer McDaniel   21          green     70  12-16-1995  1995

2)要提取月份:

>>> df['month'] = pd.DatetimeIndex(df['birth_date']).month
>>> df.head()
                              name  age favorite_color  grade  birth_date  year  month
Willard Morris      Willard Morris   20           blue     88  01-02-1996  1996      1
Al Jennings            Al Jennings   19            red     92  08-05-1997  1997      8
Omar Mullins          Omar Mullins   22         yellow     95  04-28-1996  1996      4
Spencer McDaniel  Spencer McDaniel   21          green     70  12-16-1995  1995     12

3)要提取year_with_month:

>>> df['month_year'] = pd.to_datetime(df['birth_date']).dt.to_period('M')
>>> df
                              name  age favorite_color  grade  birth_date  year  month month_year
Willard Morris      Willard Morris   20           blue     88  01-02-1996  1996      1    1996-01
Al Jennings            Al Jennings   19            red     92  08-05-1997  1997      8    1997-08
Omar Mullins          Omar Mullins   22         yellow     95  04-28-1996  1996      4    1996-04
Spencer McDaniel  Spencer McDaniel   21          green     70  12-16-1995  1995     12    1995-12

答案 2 :(得分:1)

您可以使用两种方法提取特定月份的部分数据。

<块引用>

df[df.index.month == 1]

<块引用>

df['2019-01']

两者都将只返回您想要的行。

我推荐本教程https://youtu.be/r0s4slGHwzE