gpt4 book ai didi

python - 合并 DataFrame 时丢失的条目数

转载 作者:太空宇宙 更新时间:2023-11-04 09:37:12 25 4
gpt4 key购买 nike

在一个练习中,我被要求将 3 个数据帧与内部连接合并 (df1+df2+df3 = mergedDf),然后在另一个问题中,我被要求告诉我在执行这个 3 向合并时我丢失了多少条目.

#DataFrame1
df1 = pd.DataFrame(columns=["Goals","Medals"],data=[[5,2],[1,0],[3,1]])
df1.index = ['Argentina','Angola','Bolivia']
print(df1)
Goals Medals
Argentina 5 2
Angola 1 0
Bolivia 3 1

#DataFrame2
df2 = pd.DataFrame(columns=["Dates","Medals"],data=[[1,0],[2,1],[2,2])
df2.index = ['Venezuela','Africa']
print(df2)
Dates Medals
Venezuela 1 0
Africa 2 1
Argentina 2 2

#DataFrame3
df3 = pd.DataFrame(columns=["Players","Goals"],data=[[11,5],[11,1],[10,0]])
df3.index = ['Argentina','Australia','Belgica']
print(df3)
Players Goals
Argentina 11 5
Australia 11 1
Spain 10 0

#mergedDf
mergedDf = pd.merge(df1,df2,how='inner',left_index=True, right_index=True)
mergedDf = pd.merge(mergedDf,df3,how='inner',left_index=True, right_index=True)
print(mergedDF)
Goals_X Medals_X Dates Medals_Y Players Goals_Y
Argentina 5 2 2 2 11 2

#Calculate number of lost entries by code

我尝试用外连接合并所有内容,然后减去 mergedDf,但我不知道该怎么做,有人可以帮助我吗? enter image description here

最佳答案

我找到了一个简单但有效的解决方案:

合并 3 个 DataFrame,内部和外部:

df1 = Df1()
df2 = Df2()
df3 = Df3()
inner = pd.merge(pd.merge(df1,df2,on='<Common column>',how='inner'),df3,on='<Common column>',how='inner')
outer = pd.merge(pd.merge(df1,df2,on='<Common column>',how='outer'),df3,on='<Common column>',how='outer')

现在,错过的条目(行)数是:

return (len(outer)-len(inner))

关于python - 合并 DataFrame 时丢失的条目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53227491/

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