我有以下一些数据,需要在同一行中获取星期数和年份。
data.head()
+---------------+
| epoch |
+---------------+
| 1580495399964 |
| 1580495399334 |
| 1580495397591 |
| 1580495396967 |
| 1580495396331 |
+---------------+
我如何获得这样的结果:
data.head()
+---------------+-----------+
| epoch | week-year |
+---------------+-----------+
| 1580495399964 | 4-2020 |
| 1580495399334 | 4-2020 |
| 1580495397591 | 4-2020 |
| 1580495396967 | 4-2020 |
| 1580495396331 | 4-2020 |
+---------------+-----------+
答案 0 :(得分:0)
尝试:
data['week-year'] = data.epoch.apply(pd.to_datetime, unit='ms')
data['week-year'] = data['week-year'].dt.strftime('%U-%Y')
epoch week-year
0 1580495399964 4-2020
1 1580495399334 4-2020
2 1580495397591 4-2020
3 1580495396967 4-2020
4 1580495396331 4-2020
答案 1 :(得分:-1)
下面的代码行,您可以用来生成所需格式的输出:
import time
time1 = 1580495399964
base = time.gmtime(1580495399964 / 1000)
print("{0:2}-{1:4}".format(base.tm_mon, base.tm_year))
答案 2 :(得分:-2)
data['week-year'] = pd.to_datetime(data['epoch']+19800, unit='ms').dt.strftime('%U-%Y')
+------------------+-----------+
| epoch week-year | week-year |
+------------------+-----------+
| 0 1580495399964 | 4-2020 |
| 1 1580495399334 | 4-2020 |
| 2 1580495397591 | 4-2020 |
| 3 1580495396967 | 4-2020 |
| 4 1580495396331 | 4-2020 |
+------------------+-----------+
注意:如果您的时代仅在格林尼治标准时间加
,请添加19800