使用Python在Pandas数据框中创建星期几列

时间:2015-05-13 18:24:12

标签: python datetime pandas

使用Python

在Pandas数据框中创建星期几列

我想将csv文件读入pandas数据帧,将一列日期从字符串格式解析为日期对象,然后生成一个新列,指示星期几。

这就是我正在尝试的:

我想做的是:

import pandas as pd

import csv

df = pd.read_csv('data.csv', parse_dates=['date']))

df['day-of-week'] = df['date'].weekday()


AttributeError: 'Series' object has no attribute 'weekday'

感谢您的帮助。 詹姆斯

5 个答案:

答案 0 :(得分:80)

Pandas 0.23 +

使用pandas.Series.dt.day_name(),因为pandas.Timestamp.weekday_name已被弃用:

import pandas as pd


df = pd.DataFrame({'my_dates':['2015-01-01','2015-01-02','2015-01-03'],'myvals':[1,2,3]})
df['my_dates'] = pd.to_datetime(df['my_dates'])

df['day_of_week'] = df['my_dates'].dt.day_name()

输出:

    my_dates  myvals day_of_week
0 2015-01-01       1    Thursday
1 2015-01-02       2      Friday
2 2015-01-03       3    Saturday

熊猫0.18.1 +

正如用户jezrael在下面指出的那样,在版本0.18.1中添加了dt.weekday_name Pandas Docs

import pandas as pd

df = pd.DataFrame({'my_dates':['2015-01-01','2015-01-02','2015-01-03'],'myvals':[1,2,3]})
df['my_dates'] = pd.to_datetime(df['my_dates'])
df['day_of_week'] = df['my_dates'].dt.weekday_name

输出:

    my_dates  myvals day_of_week
0 2015-01-01       1    Thursday
1 2015-01-02       2      Friday
2 2015-01-03       3    Saturday

原始答案:

使用此:

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.dayofweek.html

见:

Get weekday/day-of-week for Datetime column of DataFrame

如果你想要一个字符串而不是一个整数做这样的事情:

import pandas as pd

df = pd.DataFrame({'my_dates':['2015-01-01','2015-01-02','2015-01-03'],'myvals':[1,2,3]})
df['my_dates'] = pd.to_datetime(df['my_dates'])
df['day_of_week'] = df['my_dates'].dt.dayofweek

days = {0:'Mon',1:'Tues',2:'Weds',3:'Thurs',4:'Fri',5:'Sat',6:'Sun'}

df['day_of_week'] = df['day_of_week'].apply(lambda x: days[x])

输出:

    my_dates  myvals day_of_week
0 2015-01-01       1       Thurs
1 2015-01-02       2         Fri
2 2015-01-01       3       Thurs

答案 1 :(得分:10)

在版本0.18.1中添加了dt.weekday_name

print df
    my_dates  myvals
0 2015-01-01       1
1 2015-01-02       2
2 2015-01-03       3

print df.dtypes
my_dates    datetime64[ns]
myvals               int64
dtype: object

df['day_of_week'] = df['my_dates'].dt.weekday_name
print df
    my_dates  myvals day_of_week
0 2015-01-01       1    Thursday
1 2015-01-02       2      Friday
2 2015-01-03       3    Saturday

assign的另一个解决方案:

print df.assign(day_of_week = df['my_dates'].dt.weekday_name)
    my_dates  myvals day_of_week
0 2015-01-01       1    Thursday
1 2015-01-02       2      Friday
2 2015-01-03       3    Saturday

答案 2 :(得分:2)

使用dt.weekday_namedeprecated since pandas 0.23.0,而是使用dt.day_name()

df = pd.DataFrame({'my_dates':['2015-01-01','2015-01-02','2015-01-03'],'myvals':[1,2,3]})
df['my_dates'] = pd.to_datetime(df['my_dates'])

df['my_dates'].dt.day_name()

0    Thursday
1      Friday
2    Saturday
Name: my_dates, dtype: object

答案 3 :(得分:2)

以防万一.dt对您不起作用。尝试.DatetimeIndex可能会有所帮助。希望这里的代码和我们的测试结果可以帮助您修复它。问候

import pandas as pd
import datetime

df = pd.DataFrame({'Date':['2015-01-01','2015-01-02','2015-01-03'],'Number':[1,2,3]})

df['Day'] = pd.DatetimeIndex(df['Date']).day_name() # week day name
df.head()

enter image description here

答案 4 :(得分:0)

df =df['Date'].dt.dayofweek

dayofweek为数字格式