使用Python在Pandas dataframe中创建星期列

k75qkfdt  于 2023-04-28  发布在  Python
关注(0)|答案(7)|浏览(139)

使用Python在Pandas dataframe中创建星期列
我想将一个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'

谢谢你的帮助,詹姆斯

hwazgwia

hwazgwia1#

使用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
fiei3ece

fiei3ece2#

以防万一.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()

k7fdbhmy

k7fdbhmy3#

date为索引时

...而且只需要前三个字母:

df['day'] = df.index.day_name().str[:3]
omjgkv6w

omjgkv6w4#

data['Day_Of_Week'] = pd.DatetimeIndex(data['Birth_Date']).day_name()
命令将新列/特征追加为data ['Birth_Date']列中的data ['Day_Of-Week'],该列先前存在于dataset/csv中。

30byixjq

30byixjq5#

Pandas0.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

Pandas0.18.1+

正如用户jezrael在下面指出的,dt.weekday_name是在0.18.1 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
iyfamqjs

iyfamqjs6#

使用dt.day_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.day_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.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
ws51t4hk

ws51t4hk7#

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

dayofweek为数字格式

相关问题