如何在 Pandas 中将天数转换为小时

3

例子:

coupon             expiration
Restaurant         1d
College            2d
Coffee House       2h

输出:

coupon             expiration
Restaurant         24h
College            48h
Coffee House       2h

如何在pandas中将天数转换为小时


2
你需要使用自己的逻辑来进行字符串操作。这些不是时间或时间周期值。例如,从字符串中提取数字,乘以24,然后将其转换回字符串并添加'h'。 - Scott Boston
4个回答

1

您可以在 expiration 列上使用 str.replace 并使用正则表达式模式选择那些带有天数后缀 (d) 的条目。您还可以为 repl 参数调用函数 - 这是我选择将转换为小时的地方。

代码:

import pandas as pd

df = pd.DataFrame({"coupon":['Restaurant','College','Coffee House'], "expiration":['1d','2d','2h']})

def replacement(m):
    x = int(m.group(0).split('d')[0]) * 24
    return f"{x}h"

df.expiration = df.expiration.str.replace(pat=r'^\d+d$', repl=replacement, regex=True)
print(df)

输出:

         coupon expiration
0    Restaurant        24h
1       College        48h
2  Coffee House         2h


正则表达式模式:

r'^\d+d$'
  • ^ :字符串开头
  • \d+ :一个或多个数字[0-9]
  • d :后跟字母d
  • $ :字符串结尾

注意:

如果您更喜欢使用lambda函数的一行代码

df.expiration = df.expiration.str.replace(pat=r'^\d+d$', repl= lambda m:f"{int(m.group(0).split('d')[0]) * 24}h", regex=True)

1
你可以使用pd.to_timedelta,但是过期列中的值必须是有效的时间增量字符串:
import pandas as pd

df = pd.read_clipboard() # Your df here

tds = pd.to_timedelta(df["expiration"])
# 0   1 days 00:00:00
# 1   2 days 00:00:00
# 2   0 days 02:00:00
# Name: expiration, dtype: timedelta64[ns]

# I would recommend stopping here, but you can reformat this into a string of hours:
df["expiration"] = tds.dt.total_seconds().div(3600).apply("{:g}h".format)

#         coupon expiration
# 0   Restaurant        24h
# 1      College        48h
# 2  CoffeeHouse         2h

1
一个简单的应用可以在这里帮助到你。
def convert(x):
    if 'd' in x:
        return f"{int(x.replace('d',''))*24}h"
    return x   
df['expiration']= df['expiration'].apply(lambda x:convert(x))
df
Out[57]: 
         coupon expiration
0    Restaurant        24h
1       College        48h
2  Coffee House         2h

1

另一种可能的解决方案,基于eval

df['expiration'] = [str(eval(x)) + 'h' for x in
                    df['expiration'].str.replace('d', '*24').str.replace('h', '')]

输出:

         coupon expiration
0    Restaurant        24h
1       College        48h
2  Coffee House         2h

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接