我的DataFrame具有“时间”列。像
这样的数据['Mon-Sun', 'Mon, Tue, Thu, Fri, Sat, Sun',
'Mon, Tue, Wed, Thu, Fri, Sun', 'Tue-Sun',
'Mon, Tue, Wed, Thu, Sun', 'Mon-Thu', 'Sun', 'Fri-Sun', nan, 'Tue',
'Mon, Wed', 'Mon-Fri', 'Mon', 'Mon-Sat',
'Mon, Wed, Thu, Fri, Sat, Sun', 'Mon, Tue, Wed, Fri, Sat, Sun',
'Sat-Sun', 'Wed-Sun', 'Mon-Wed', 'Mon, Tue, Fri, Sat, Sun',
'Mon, Tue, Thu', 'Mon, Tue, Wed, Thu, Sat, Sun', 'Mon, Wed, Fri',
'Tue-Sat', 'Sat', 'Mon, Sun', 'Mon, Wed, Thu, Fri',
'Mon, Thu, Fri, Sat, Sun', 'Mon, Thu', 'Mon, Tue, Wed, Fri',
'Mon, Tue, Thu, Sun', 'Fri', 'Mon-Tue', 'Thu-Sun', 'Tue-Fri',
'Mon, Tue, Wed, Sun', 'Mon, Tue, Sat, Sun',
'Mon, Wed, Thu, Fri, Sun', 'Mon, Wed, Thu, Sun',
'Mon, Tue, Thu, Fri', 'Mon, Tue, Thu, Fri, Sat', 'Wed',
'Mon, Tue, Wed, Thu, Sat', 'Fri-Sat', 'Mon, Wed, Thu',
'Mon, Tue, Wed, Sat, Sun', 'Mon, Wed, Fri, Sat, Sun', 'Tue-Thu',
'Mon, Tue, Sun', 'Mon, Fri, Sat, Sun', 'Mon, Sat', 'Thu-Sat',
'Mon, Sat, Sun', 'Thu', 'Mon, Fri', 'Tue, Sat, Sun', 'Wed-Sat',
'Mon, Wed, Thu, Fri, Sat', 'Mon, Wed, Thu, Sat', 'Mon, Thu, Sat',
'Mon, Thu, Fri', 'Mon, Fri, Sat', 'Tue, Wed, Thu, Sun']
需要在DataFrame中创建其他列,即Mon,Tue,Wed,Thurs,Fri,Sat,Sun,并在每个列中插入1或0(如果将TIME列作为工作日)。
我已经创建了所述列
预期结果
TIME. Mon Tue Wed Thur Fri Sat Sun.
Mon-Sun. 1. 1. 1. 1. 1. 1. 1
Mon, Tue, Thu, Fri, Sat, Sun. 1. 1. 0 1 1. 1 1
答案 0 :(得分:0)
data = ['Mon-Sun', 'Mon, Tue, Thu, Fri, Sat, Sun',
'Mon, Tue, Wed, Thu, Fri, Sun', 'Tue-Sun',
'Mon, Tue, Wed, Thu, Sun', 'Mon-Thu', 'Sun', 'Fri-Sun', None, 'Tue',
'Mon, Wed', 'Mon-Fri', 'Mon', 'Mon-Sat',
'Mon, Wed, Thu, Fri, Sat, Sun', 'Mon, Tue, Wed, Fri, Sat, Sun',
'Sat-Sun', 'Wed-Sun', 'Mon-Wed', 'Mon, Tue, Fri, Sat, Sun',
'Mon, Tue, Thu', 'Mon, Tue, Wed, Thu, Sat, Sun', 'Mon, Wed, Fri',
'Tue-Sat', 'Sat', 'Mon, Sun', 'Mon, Wed, Thu, Fri',
'Mon, Thu, Fri, Sat, Sun', 'Mon, Thu', 'Mon, Tue, Wed, Fri',
'Mon, Tue, Thu, Sun', 'Fri', 'Mon-Tue', 'Thu-Sun', 'Tue-Fri',
'Mon, Tue, Wed, Sun', 'Mon, Tue, Sat, Sun',
'Mon, Wed, Thu, Fri, Sun', 'Mon, Wed, Thu, Sun',
'Mon, Tue, Thu, Fri', 'Mon, Tue, Thu, Fri, Sat', 'Wed',
'Mon, Tue, Wed, Thu, Sat', 'Fri-Sat', 'Mon, Wed, Thu',
'Mon, Tue, Wed, Sat, Sun', 'Mon, Wed, Fri, Sat, Sun', 'Tue-Thu',
'Mon, Tue, Sun', 'Mon, Fri, Sat, Sun', 'Mon, Sat', 'Thu-Sat',
'Mon, Sat, Sun', 'Thu', 'Mon, Fri', 'Tue, Sat, Sun', 'Wed-Sat',
'Mon, Wed, Thu, Fri, Sat', 'Mon, Wed, Thu, Sat', 'Mon, Thu, Sat',
'Mon, Thu, Fri', 'Mon, Fri, Sat', 'Tue, Wed, Thu, Sun']
import pandas as pd
header = ['TIME.', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
result = []
for week in data:
result_row = [week] + [0]*7
if week is not None:
week = week.replace(" ", "")
for entry in week.split(','):
if len(entry.split('-'))>1:
start_idx = header.index(entry.split('-')[0])
end_idx = header.index(entry.split('-')[1])+1
for i in range(start_idx, end_idx):
result_row[i] = 1
else:
idx = header.index(entry)
result_row[idx] = 1
result.append(result_row)
df = pd.DataFrame(result, columns=header)
print(df)
TIME. Mon Tue Wed Thu Fri Sat Sun
0 Mon-Sun 1 1 1 1 1 1 1
1 Mon, Tue, Thu, Fri, Sat, Sun 1 1 0 1 1 1 1
2 Mon, Tue, Wed, Thu, Fri, Sun 1 1 1 1 1 0 1
3 Tue-Sun 0 1 1 1 1 1 1
4 Mon, Tue, Wed, Thu, Sun 1 1 1 1 0 0 1
5 Mon-Thu 1 1 1 1 0 0 0
6 Sun 0 0 0 0 0 0 1
7 Fri-Sun 0 0 0 0 1 1 1
8 None 0 0 0 0 0 0 0
9 Tue 0 1 0 0 0 0 0
10 Mon, Wed 1 0 1 0 0 0 0
11 Mon-Fri 1 1 1 1 1 0 0
12 Mon 1 0 0 0 0 0 0
13 Mon-Sat 1 1 1 1 1 1 0
14 Mon, Wed, Thu, Fri, Sat, Sun 1 0 1 1 1 1 1
15 Mon, Tue, Wed, Fri, Sat, Sun 1 1 1 0 1 1 1
16 Sat-Sun 0 0 0 0 0 1 1
...