gpt4 book ai didi

python - 检查列表中两个 Dataframe 列中的数据是否存在

转载 作者:行者123 更新时间:2023-12-04 07:28:08 25 4
gpt4 key购买 nike

我正在寻找 difference DataFrame 中的列之间和 List 中的数据.
我是这样做的:


# pickled_data => list of dics

pickled_names = [d['company'] for d in pickled_data] # get values from dictionary to list
diff = df[~df['company_name'].isin(pickled_names)]

效果很好,但我意识到我不仅需要检查 company_name也适用于 place ,因为可能有两家公司名称相同。 df还包含列 place以及 pickled_data包含 place字典中的键。
我希望能够做这样的事情
pickled_data = [(d['company'], d['place']) for d in pickled_data]

diff = df[~df['company_name', 'place'].isin(pickled_data)] # For two values in same row

最佳答案

您可以将值转换为 MultiIndex来自 MultiIndex.from_tuples ,然后也转换两列并比较:

pickled_data = [(d['company'], d['place']) for d in pickled_data]

mux = pd.MultiIndex.from_tuples(pickled_data)

diff = df[~df.set_index(['company_name', 'place']).index.isin(mux)]
sample :
data = {'company_name':['A1','A2','A2','A1','A1','A3'],
'place':list('sdasas')}
df = pd.DataFrame(data)


pickled_data = [('A1','s'),('A2','d')]

mux = pd.MultiIndex.from_tuples(pickled_data)

diff = df[~df.set_index(['company_name', 'place']).index.isin(mux)]

print (diff)
company_name place
2 A2 a
4 A1 a
5 A3 s

关于python - 检查列表中两个 Dataframe 列中的数据是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68110416/

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