如何将字段名为变量的python数据帧传递给自定义函数

2024-10-01 17:28:20 发布

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

我有2个dataframe,DataFrame1:locationlisexport-Columns:['CountryCode','Country Name'] Dataframe2:位置-列:['Code','Name']

我想找到LocationListExport['CountryCode'],它不在Location['Code']中

我的职能:

def checkDimensionListItems(dataDL, dDLdimensionCode, knoxDimensionWithCode):
newDimensionItemAdded = dataDL[~dataDL[dDLdimensionCode].isin(knoxDimensionWithCode)][dDLdimensionCode].unique()
return newDimensionItemAdded

调用函数:

checkDimensionListItems(LocationListExport, 'CountryCode', 'Location.Code')

发生错误:AttributeError:TypeError:仅允许将类似列表的对象传递给isin(),您传递了一个[str]

下面的脚本在没有函数的情况下工作得很好,但是我想更新许多数据帧。你知道吗

LocationListExport[~LocationListExport.CountryCode.isin(Location.Code)].CountryCode.unique()

我们可以使用用户定义的函数来实现这一点。你知道吗


Tags: 函数namedataframecodelocationuniquecountrycodeisin
1条回答
网友
1楼 · 发布于 2024-10-01 17:28:20

为DF&Field添加2个变量,如下所示:

def checkDimensionListItems(dataDL, dDLdimensionCode, knoxDimension, knoxDimensionCode):
newDimensionItemAdded = dataDL[~dataDL[dDLdimensionCode].isin(knoxDimension[knoxDimensionCode])]

return newDimensionItemAdded

checkDimensionListItems(LocationListExport,'国家代码',Location,'代码')

相关问题 更多 >

    热门问题