简体   繁体   English

将函数应用于每行python

[英]Apply a function to each row python

I am trying to convert from UTC time to LocaleTime in my dataframe. 我正在尝试从数据帧中的UTC时间转换为LocaleTime。 I have a dictionary where I store the number of hours I need to shift for each country code. 我有一dictionary ,用于存储每个国家/地区代码需要转移的小时数。 So for example if I have df['CountryCode'][0]='AU' and I have a df['UTCTime'][0]=2016-08-12 08:01:00 I want to get df['LocaleTime'][0]=2016-08-12 19:01:00 which is 因此,例如,如果我有df['CountryCode'][0]='AU'并且我有df['UTCTime'][0]=2016-08-12 08:01:00我想获取df['LocaleTime'][0]=2016-08-12 19:01:00这是

df['UTCTime'][0]+datetime.timedelta(hours=dateDic[df['CountryCode'][0]])

I have tried to do it with a for loop but since I have more than 1 million rows it's not efficient. 我尝试过使用for loop来完成此操作,但是由于我有超过一百万行,因此效率不高。 I have looked into the apply function but I can't seem to be able to put it to take inputs from two different columns. 我已经研究了apply函数,但似乎无法将其用于接受来自两个不同列的输入。

Can anyone help me? 谁能帮我?

如果没有更具体的示例,这很困难,但是请尝试以下操作:

pd.to_timedelta(df.CountryCode.map(dateDict), 'h') + df.UTCTime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM