gpt4 book ai didi

python - 无法加入指定参数的 pandas 数据框

转载 作者:行者123 更新时间:2023-12-01 08:18:31 25 4
gpt4 key购买 nike

我需要在索引上对两个数据帧进行内部连接

objectId label  
46943 1
96057 1
98476 1
121042 1
124587 1

objectId preprocessed
3873375 [завоева, ве, мир, род, помидорчик, черр, наш,...
10805823 [российск, академ, наук, ран, призна, слов, со...
19557054 []
19750631 [зарегистрирова, куйбышев, прошедш, недел, пре...
24316030 [салат, русск, красавиц, ингредиент, курин, гр...

当我尝试打电话时

ids.join(texts, how = 'inner', on = 'objectId')

我收到错误:

 KeyError: 'objectId' 

但是 ids.join(texts, how = 'inner') 工作正常。不明白为什么。

最佳答案

函数join默认按索引合并,因此如果objectId不是列,而是索引,它会正确合并。

<小时/>

如果使用:

ids.join(texts, how = 'inner', on = 'objectId')

表示将索引加入objectId列,如果不存在,则得到keyerror

ids = pd.DataFrame({
'objectId':[1,3,5,7,10,0],
'label':[5,3,6,9,2,4],

})

texts = pd.DataFrame({
'objectId':[10,3,5],
'preprocessed':[['d','ff'],['gg','dd'],['qq']],

})

print (texts)

#index from objectId column
texts = texts.set_index('objectId')
#join column ids.objectId to index texts.index
df = ids.join(texts, how = 'inner', on = 'objectId')
print (df)
objectId label preprocessed
1 3 3 [gg, dd]
2 5 6 [qq]
4 10 2 [d, ff]

关于python - 无法加入指定参数的 pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54842666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com