Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python divmod()不支持的操作数类型:';str';和';int';格式化SQL结果时_Python - Fatal编程技术网

Python divmod()不支持的操作数类型:';str';和';int';格式化SQL结果时

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 *

我想格式化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 * 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)