例子:
coupon expiration
Restaurant 1d
College 2d
Coffee House 2h
输出:
coupon expiration
Restaurant 24h
College 48h
Coffee House 2h
如何在pandas中将天数转换为小时
例子:
coupon expiration
Restaurant 1d
College 2d
Coffee House 2h
输出:
coupon expiration
Restaurant 24h
College 48h
Coffee House 2h
如何在pandas中将天数转换为小时
您可以在 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)
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
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
另一种可能的解决方案,基于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