将df中的列从另一列更改为列

时间:2016-06-23 11:13:28

标签: python excel datetime pandas dataframe

我有数据:

                 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添加到第一个文件datehours。 欲望输出:

                     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转换为小时。

2 个答案:

答案 0 :(得分:2)

如果date不是dtype,则需要先转换to_datetimedatetimeidEmailUTC shift 。然后转换mergedate,添加到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')