【问题标题】:Pandas Left Join results in more rows than the Left DataframePandas Left Join 产生的行数比 Left Dataframe 多
【发布时间】:2021-03-22 09:52:33
【问题描述】:

结果左连接中的行数比左数据框中的行数多。

# Importing Pandas and changing it's call to pd
import numpy as np
import pandas as pd

SalesDF = pd.read_csv(r"C:\Users\USER\Documents\Reports\SalesForAnalysis.csv")
print("This is the Sales shape")
print(SalesDF.shape)


CustInfoDF = pd.read_csv(r"C:\Users\USER\Documents\Cust.csv")

# This reassigns the df so that the rows with a NaN in the Account Number it  doesn't appear
CustInfoDF = CustInfoDF[CustInfoDF['Account Number'].notna()]


# Merges the two dataframes on SalesDF with "Cust Number" as the key
MergeDF = pd.merge(SalesDF, CustInfoDF, how="left", left_on="Cust Number", right_on="Account Number")

print("This is the Merge Shape ")
print(MergeDF.shape)

# Reduced the number of columns to the selected columns
CutDF = MergeDF[["Customer", "Invoice #", "E-mail Address", "Phone", "Clerk", "Total", "Date"]]

CutDF.drop_duplicates()

print("This is the Cut shape ")
print(CutDF.shape)


这是运行程序后的结果

This is the Sales shape
(5347, 61)
This is the Merge Shape 
(6428, 83)
This is the Cut shape 
(6428, 7)

Process finished with exit code 0

CutDF 最多只能有 5347 行。我有一个 drop_duplicates 方法,但我仍然得到相同的结果。

我看到了这个pandas left join - why more results?inner join/merge in pandas dataframe give more rows than left dataframe,但我并没有真正在这些中看到解决方案。

任何帮助将不胜感激。

【问题讨论】:

  • 什么是SalesDF['Cust Number'].duplicated().any()
  • 恐怕我不明白这个问题。你能改写一下吗?

标签: python pandas dataframe merge left-join


【解决方案1】:

执行之前:

MergeDF = pd.merge(SalesDF, CustInfoDF, how="left", left_on="Cust Number", right_on="Account Number")

你能做到吗:

CustInfoDF = CustInfoDF.drop_duplicates(subset=["Account Number"])

我怀疑您的 CustInforDF 对于每个 Account Number 都有多个条目。

如果这不起作用,您能否发布示例数据框?只要代码是可复制的,就可以随意添加/替换为虚拟值。

【讨论】:

  • 您的假设是正确的,即存在多余的帐号。当我有时间时,我会更加认真地实现这一点。
  • 在我的 MergeDF 形成之前添加 CustInfoDF = CustInfoDF.drop_duplicates(on=["Account Number"]) 后我的新结果消息 ``` 这是销售形状 (5104, 62) Traceback(最近一次通话最后一次):文件“C:\Users\ USER\PycharmProjects\PandasStuff\PyMailReport.py",第 18 行,在 CustInfoDF = CustInfoDF.drop_duplicates(on=["Account Number"]) TypeError: drop_duplicates() got an unexpected keyword argument 'on' Process finished with exit代码 1```
  • 最终评论 谢谢 Parmandeep,问题已解决。 drop_duplicates 代码中不需要“on =”,这就是抛出程序的原因,但现在它似乎运行良好。非常感谢
  • 啊,是的,抱歉 on 应该是 subset。只是更新以防人们将来使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 2018-09-04
  • 2020-09-19
  • 1970-01-01
  • 2023-01-02
  • 2018-07-10
  • 2019-10-09
相关资源
最近更新 更多