将用户定义函数应用于DataFrame失败,Python3.6

2024-09-27 21:27:47 发布

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

我有下面的数据框,想从字段“Indicator\u NameB”中分离日期和文本,我编写了函数“String\u Year\u Pick\u V2”

数据:

    Indicator_NameB
       2011-12 (RE)
       2012-13 (BE)
        2007-08
    Approved Outlay
Total for 11th Plan

Data['Indicator_NameB'].map(str).apply(String_Year_Pick_V2)

KN.String_Year_Pick_V2-此函数从“Indicator\u NameB”的每个值中分离文本和日期,并作为序列值返回,如(RE), 2011-12

获取以下错误:

AttributeError: 'builtin_function_or_method' object has no attribute 'get_indexer'

然后我更新到数据帧:

DataT[['Indicator_NameB', 'Year']] = DataT['Indicator_NameB'].map(str).apply(String_Year_Pick_V2)

我怀疑

String_Year_Pick_V2('2007-08') gives '', '2007-08'
String_Year_Pick_V2('Approved Outlay') gives 'Approved Outlay', ''

可能是以上结果导致错误


Tags: 数据函数文本remapstringyearindicator
1条回答
网友
1楼 · 发布于 2024-09-27 21:27:47

String\u Year\u Pick\u V2-函数有多个返回语句,我假设返回序列值,有些代码返回“String”、“Year”作为变量,这就造成了错误。你知道吗

相关问题 更多 >

    热门问题