我有一个数据框:
Year Month Week_of_month Day_of_week
0 2018 1 2 1
1 2018 1 1 2
2 2018 1 2 2
3 2018 1 1 3
4 2018 1 2 3
5 2018 1 1 4
6 2018 1 2 4
7 2018 1 1 5
8 2018 1 2 5
9 2018 1 1 6
10 2018 1 2 6
11 2018 1 1 7
其中包含:
星期几(1到7->星期日至星期六)
每月的一周(1到6-一个月最多6周)
我想根据这4条信息获得相应的日期:
例如,要获得30th of december of 2018
,我这样做:
我使用了此功能:
calendar.setfirstweekday(calendar.SUNDAY)
calendar.monthcalendar(2018,12)
[[0, 0, 0, 0, 0, 0, 1],
[2, 3, 4, 5, 6, 7, 8],
[9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22],
[23, 24, 25, 26, 27, 28, 29],
[30, 31, 0, 0, 0, 0, 0]]
calendar.monthcalendar(2018,12)[5][1]
31
重点是: 如何使用此功能获取数据框的每一行中的每一天?
我尝试过:
df['Day'] = calendar.monthcalendar(df.Year, df.Month)[df.Week_of_month][df.Day_of_week]
但是,我遇到了错误:
TypeError: '>=' not supported between instances of 'str' and 'int'
答案 0 :(得分:1)
您在这里:
df['Day'] = df.apply(lambda x: calendar.monthcalendar(x.Year, x.Month)[x.Week_of_month-1][x.Day_of_week-1], axis=1)
输出:
https://github.com/facebook/react-native/issues/22118
希望这会有所帮助。