参与python中的字符串

2024-06-16 10:08:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有个错误Can only use .str accessor with string values (i.e. inferred_type is 'string', 'unicode' or 'mixed') 对于此代码

newestdata = newestdf.assign(
        idobject=newestdf.index.str.split('/').str[1].str.replace("-", "").str.extract('(\d+)', expand=False).astype(int))

我曾经参与其中的一部分:

OOOO-ASAS/INTEL-64646/OOOO-15445/PPPO-9

但在一个python脚本中是这样的,但在另一个脚本中不是这样的,它工作得很好。你知道问题出在哪里吗?你知道吗


Tags: 脚本onlystringisusetype错误with
1条回答
网友
1楼 · 发布于 2024-06-16 10:08:11

有一个问题,你有混合数据-一些数字与stringsindex。你知道吗

需要转换为string作为第一步:

newestdata = newestdf.assign(
        idobject=newestdf.index.astype(str).str.split('/').str[1].str.replace("-", "").str.extract('(\d+)', expand=False).astype(int))
                                 ^^^^^^^^^^

相关问题 更多 >