1 回答
TA贡献1831条经验 获得超10个赞
pandas 有一个内置函数,称为day_name:
# sample data
df = pd.DataFrame(pd.date_range('2019-01-01', '2019-01-10'))
# use day_name
df['day of week'] = df[0].dt.day_name()
0 day of week
0 2019-01-01 Tuesday
1 2019-01-02 Wednesday
2 2019-01-03 Thursday
3 2019-01-04 Friday
4 2019-01-05 Saturday
5 2019-01-06 Sunday
6 2019-01-07 Monday
7 2019-01-08 Tuesday
8 2019-01-09 Wednesday
9 2019-01-10 Thursday
或者你可以map用一个字典:
# sample data
df = pd.DataFrame(pd.date_range('2019-01-01', '2019-01-10'))
# int of day of week
df['day_of_week'] = df[0].dt.dayofweek
# map with a dict
df['day_of_week_name'] = df['day_of_week'].map({0:'mon', 1:'tue',
2:'wed', 3:'thur',
4:'fri', 5:'sat', 6:'sun'})
0 day_of_week day_of_week_name
0 2019-01-01 1 tue
1 2019-01-02 2 wed
2 2019-01-03 3 thur
3 2019-01-04 4 fri
4 2019-01-05 5 sat
5 2019-01-06 6 sun
6 2019-01-07 0 mon
7 2019-01-08 1 tue
8 2019-01-09 2 wed
9 2019-01-10 3 thur
添加回答
举报