我有数据:
date id
0 2016-06-17 06:25:05 yans.bouts@yandex.ru
1 2016-06-17 06:25:07 yans.bouts@yandex.ru
2 2016-06-17 06:25:10 titovtanya@yandex.ru
3 2016-06-17 06:25:11 titovtanya@yandex.ru
其他数据
Email,UTC shift
yans.bouts@yandex.ru,5
inkin_sam@mail.ru,3
titovtanya@yandex.ru,3
dasha.dasha.kovaleva@mail.ru,2
我需要将UTC shift
添加到第一个文件date
到hours
。
欲望输出:
date id
0 2016-06-17 11:25:05 yans.bouts@yandex.ru
1 2016-06-17 11:25:07 yans.bouts@yandex.ru
2 2016-06-17 09:25:10 titovtanya@yandex.ru
3 2016-06-17 09:25:11 titovtanya@yandex.ru
我将date
转换为日期时间,但我不知道如何将UTC shift
转换为小时。
答案 0 :(得分:2)
如果date
不是dtype
,则需要先转换to_datetime
列datetime
,id
列Email
和UTC shift
。然后转换merge
列date
,添加到import pandas as pd
df1 = pd.DataFrame({'date': {0: '2016-06-17 06:25:05', 1: '2016-06-17 06:25:07', 2: '2016-06-17 06:25:10', 3: '2016-06-17 06:25:11'},
'id': {0: 'yans.bouts@yandex.ru', 1: 'yans.bouts@yandex.ru', 2: 'titovtanya@yandex.ru', 3: 'titovtanya@yandex.ru'}})
df2 = pd.DataFrame({'Email': {0: 'yans.bouts@yandex.ru', 1: 'inkin_sam@mail.ru', 2: 'titovtanya@yandex.ru', 3: 'dasha.dasha.kovaleva@mail.ru'},
'UTC shift': {0: 5, 1: 3, 2: 3, 3: 2}})
print (df1)
date id
0 2016-06-17 06:25:05 yans.bouts@yandex.ru
1 2016-06-17 06:25:07 yans.bouts@yandex.ru
2 2016-06-17 06:25:10 titovtanya@yandex.ru
3 2016-06-17 06:25:11 titovtanya@yandex.ru
print (df2)
Email UTC shift
0 yans.bouts@yandex.ru 5
1 inkin_sam@mail.ru 3
2 titovtanya@yandex.ru 3
3 dasha.dasha.kovaleva@mail.ru 2
和最后to_timedelta
个不必要的列:
df1['date'] = pd.to_datetime(df1.date)
df = pd.merge(df1, df2, left_on='id', right_on='Email')
df['date'] += pd.to_timedelta(df['UTC shift'], unit='H')
df.drop(['Email','UTC shift'], axis=1, inplace=True)
print (df)
date id
0 2016-06-17 11:25:05 yans.bouts@yandex.ru
1 2016-06-17 11:25:07 yans.bouts@yandex.ru
2 2016-06-17 09:25:10 titovtanya@yandex.ru
3 2016-06-17 09:25:11 titovtanya@yandex.ru
onPause
答案 1 :(得分:1)
试试这个:
df['date'] += pd.Timedelta(df['UTC shift'], unit='H')