与 Pandas 一起命名日
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29096381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Num day to Name day with Pandas
提问by Yari
If I use this funtion pd.DatetimeIndex(dfTrain['datetime']).weekdayI get number of the day, but I don't find any function which give the name of de day... So I need to convert 0 to Monday, 1 to Tuestday and so on.
如果我使用这个函数,pd.DatetimeIndex(dfTrain['datetime']).weekday我会得到当天的编号,但我没有找到任何给出日期名称的函数......所以我需要将 0 转换为星期一,将 1 转换为星期二等等。
Here is an example of my dataframe:
这是我的数据框的示例:
datetime season holiday workingday weather temp atemp humidity windspeed count
0 2011-01-01 00:00:00 1 0 0 1 9.84 14.395 81 0.0000 16
1 2011-01-01 01:00:00 1 0 0 1 9.02 13.635 80 0.0000 40
2 2011-01-01 02:00:00 1 0 0 1 9.02 13.635 80 0.0000 32
3 2011-01-01 03:00:00 1 0 0 1 9.84 14.395 75 0.0000 13
4 2011-01-01 04:00:00 1 0 0 1 9.84 14.395 75 0.0000 1
5 2011-01-01 05:00:00 1 0 0 2 9.84 12.880 75 6.0032 1
6 2011-01-01 06:00:00 1 0 0 1 9.02 13.635 80 0.0000 2
7 2011-01-01 07:00:00 1 0 0 1 8.20 12.880 86 0.0000 3
8 2011-01-01 08:00:00 1 0 0 1 9.84 14.395 75 0.0000 8
9 2011-01-01 09:00:00 1 0 0 1 13.12 17.425 76 0.0000 14
Another question more, which is the difference between pandas.DatetimeIndex.dayofweekand pandas.DatetimeIndex.weekday?
还有一个问题,pandas.DatetimeIndex.dayofweek和之间的区别是pandas.DatetimeIndex.weekday什么?
回答by EdChum
One method, so long as datetime is already a datetime column is to apply datetime.strftimeto get the string for the weekday:
一种方法,只要 datetime 已经是 datetime 列,就可以申请datetime.strftime获取工作日的字符串:
In [105]:
df['weekday'] = df[['datetime']].apply(lambda x: dt.datetime.strftime(x['datetime'], '%A'), axis=1)
df
Out[105]:
datetime season holiday workingday weather temp atemp \
0 2011-01-01 00:00:00 1 0 0 1 9.84 14.395
1 2011-01-01 01:00:00 1 0 0 1 9.02 13.635
2 2011-01-01 02:00:00 1 0 0 1 9.02 13.635
3 2011-01-01 03:00:00 1 0 0 1 9.84 14.395
4 2011-01-01 04:00:00 1 0 0 1 9.84 14.395
5 2011-01-01 05:00:00 1 0 0 2 9.84 12.880
6 2011-01-01 06:00:00 1 0 0 1 9.02 13.635
7 2011-01-01 07:00:00 1 0 0 1 8.20 12.880
8 2011-01-01 08:00:00 1 0 0 1 9.84 14.395
9 2011-01-01 09:00:00 1 0 0 1 13.12 17.425
humidity windspeed count weekday
0 81 0.0000 16 Saturday
1 80 0.0000 40 Saturday
2 80 0.0000 32 Saturday
3 75 0.0000 13 Saturday
4 75 0.0000 1 Saturday
5 75 6.0032 1 Saturday
6 80 0.0000 2 Saturday
7 86 0.0000 3 Saturday
8 75 0.0000 8 Saturday
9 76 0.0000 14 Saturday
As to your other question, there is no difference between dayofweekand weekday.
至于你的另一个问题,dayofweek和之间没有区别weekday。
It will be quicker to define a map of the weekday to String equivalent and call map on the weekday:
将工作日的映射定义为等效的字符串并在工作日调用映射会更快:
dayOfWeek={0:'Monday', 1:'Tuesday', 2:'Wednesday', 3:'Thursday', 4:'Friday', 5:'Saturday', 6:'Sunday'}
df['weekday'] = df['datetime'].dt.dayofweek.map(dayOfWeek)
For version prior to 0.15.0the following should work:
对于0.15.0以下版本之前的版本应该可以工作:
import datetime as dt
df['weekday'] = df['datetime'].apply(lambda x: dt.datetime.strftime(x, '%A'))
Version 0.18.1 and newer
0.18.1 及更新版本
There is now a new convenience method dt.weekday_nameto do the above
现在有一种新的便捷方法dt.weekday_name可以执行上述操作
Version 0.23.0 and newer
0.23.0 及更新版本
weekday_name is now depricated in favour of dt.day_name.
weekday_name 现在被弃用了,取而代之的是dt.day_name.
回答by jezrael
In version 0.18.1you can use new method dt.weekday_name:
在版本中,0.18.1您可以使用新方法dt.weekday_name:
df['weekday'] = df['datetime'].dt.weekday_name
print df
datetime season holiday workingday weather temp atemp \
0 2011-01-01 00:00:00 1 0 0 1 9.84 14.395
1 2011-01-01 01:00:00 1 0 0 1 9.02 13.635
2 2011-01-01 02:00:00 1 0 0 1 9.02 13.635
3 2011-01-01 03:00:00 1 0 0 1 9.84 14.395
4 2011-01-01 04:00:00 1 0 0 1 9.84 14.395
5 2011-01-01 05:00:00 1 0 0 2 9.84 12.880
6 2011-01-01 06:00:00 1 0 0 1 9.02 13.635
7 2011-01-01 07:00:00 1 0 0 1 8.20 12.880
8 2011-01-01 08:00:00 1 0 0 1 9.84 14.395
9 2011-01-01 09:00:00 1 0 0 1 13.12 17.425
humidity windspeed count weekday
0 81 0.0000 16 Saturday
1 80 0.0000 40 Saturday
2 80 0.0000 32 Saturday
3 75 0.0000 13 Saturday
4 75 0.0000 1 Saturday
5 75 6.0032 1 Saturday
6 80 0.0000 2 Saturday
7 86 0.0000 3 Saturday
8 75 0.0000 8 Saturday
9 76 0.0000 14 Saturday
回答by user3483203
Using dt.weekday_nameis deprecated since pandas 0.23.0, instead, use dt.day_name():
Usingdt.weekday_name已被弃用,因为pandas 0.23.0,而是使用dt.day_name():
df.datetime.dt.day_name()
0 Saturday
1 Saturday
2 Saturday
3 Saturday
4 Saturday
5 Saturday
6 Saturday
7 Saturday
8 Saturday
9 Saturday
Name: datetime, dtype: object
回答by Neofytos Boufidis
Adding to the previous correctanswer from @jezrael, you can use this:
添加到来自@jezrael的先前正确答案,您可以使用:
import calendar
df['weekday'] = pd.Series(pd.Categorical(df['datetime'].dt.weekday_name, categories=list(calendar.day_name)))
which also provides your new categorical variable with order(in this example: 'Monday', ..., 'Sunday') according to this. This will possibly be helpful on next steps of your analysis.
它也提供了新的分类变量与顺序(在本例:“周一,...,”周日)根据本。这可能对您分析的后续步骤有所帮助。

