Python divmod()不支持的操作数类型:';str';和';int';格式化SQL结果时
我想格式化SQL查询的结果: 原始数据: 名称 天 爱丽丝 60 上下快速移动 52 迈克 266 卢卡斯 27 需要数字作为参数,因此请在此处将Python divmod()不支持的操作数类型:';str';和';int';格式化SQL结果时,python,Python,我想格式化SQL查询的结果: 原始数据: 名称 天 爱丽丝 60 上下快速移动 52 迈克 266 卢卡斯 27 需要数字作为参数,因此请在此处将days转换为数字: get_days=get_days(int(data.days)) ^^^^ 或在此: 商,模数=divmod(整数(天),30) ^^^^ 创建两个系列,分别命名为月和月中日。然后,根据您的条件对其进行格式化 months = df.days // 30 day_in_month = df.days - (months *
days
转换为数字:
get_days=get_days(int(data.days))
^^^^
或在此:
商,模数=divmod(整数(天),30)
^^^^
创建两个系列,分别命名为
月
和月中日
。然后,根据您的条件对其进行格式化
months = df.days // 30
day_in_month = df.days - (months * 30)
conditions = [
(months == 0) & (day_in_month == 0),
(months == 0) & (day_in_month != 0),
(months != 0) & (day_in_month == 0)
]
values = [np.nan, day_in_month.astype(str).add('/30'), months]
default = months.astype(str).add(' ').add(day_in_month.astype(str)).add('/30')
df['days_formatted'] = np.select(conditions, values, default)
输出
name days days_formatted
0 Alice 60 2
1 Bob 52 1 22/30
2 Mike 266 8 26/30
3 Lucas 27 27/30
在
get_days
函数中,days
的数据类型是什么?使用打印(类型(天))
。这可能是str
类型,divmod
需要int
。您需要将str
类型转换为int
:商,模数=divmod(int(days),30)