pandas提取日期的年份,月份

import pandas as pd
list_of_dates = ['2019-11-20', '2020-01-02', '2020-02-05','2020-03-10','2020-04-16']
employees=['Hisila', 'Shristi','Zeppy','Alina','Jerry']
df = pd.DataFrame({ 
'Joined date': pd.to_datetime(list_of_dates)}, index=employees)
df['Year'] = df['Joined date'].dt.year
df['Month'] = df['Joined date'].dt.month
print(df)

关键函数:df['Month'] = df['Joined date'].dt.month

筛选满足多条件的数据:

import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt

path = './0area_jrc_raw'
filenames = os.listdir(path)

for filename in filenames:
    file_path = r'./0area_jrc_raw/' + filename
    df = pd.read_excel(file_path)
    
    df['Month'] = df['Date'].dt.month
    
    data = df[(df['Month'] >5) & (df['Month']<12)] #select 6-11 month data
    
    print(data['Month'])
    原文作者:RS&Hydrology
    原文地址: https://blog.csdn.net/qq_32649321/article/details/123003599
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞