{
  "id": 134645,
  "title": "Pandas Left merge results in table larger than left table",
  "url": "/competitions/google-cloud-ncaa-march-madness-2020-division-1-womens-tournament/discussion/134645",
  "author_name": "",
  "post_date": "2020-03-09T13:51:42.539389900Z",
  "votes": null,
  "comment_count": 3,
  "views": 0,
  "content": "<p>I used Pandas' merge () function to combine test data and game city data.\n<code>\ntest_df = test_df.merge(GameCities,how='left',on=['Season','WTeamID','LTeamID'])\n</code>\nAs a result, the test data was converted from 10080 rows to 10200 rows.  But the training data could be merged without increasing the rows.\nI found that this could increase if the key matched multiple rows in other DataFrames.\n<a href=\"https://stackoverflow.com/questions/22720739/pandas-left-outer-join-results-in-table-larger-than-left-table\">Reference</a>\nIs there a way to prevent rows from increasing and combine them?</p>",
  "messages": [
    {
      "id": "767339",
      "postDate": "03/09/2020 13:51:42",
      "content": "<p>I used Pandas' merge () function to combine test data and game city data.\n<code>\ntest_df = test_df.merge(GameCities,how='left',on=['Season','WTeamID','LTeamID'])\n</code>\nAs a result, the test data was converted from 10080 rows to 10200 rows.  But the training data could be merged without increasing the rows.\nI found that this could increase if the key matched multiple rows in other DataFrames.\n<a href=\"https://stackoverflow.com/questions/22720739/pandas-left-outer-join-results-in-table-larger-than-left-table\">Reference</a>\nIs there a way to prevent rows from increasing and combine them?</p>",
      "rawMarkdown": "I used Pandas' merge () function to combine test data and game city data.\n```\ntest_df = test_df.merge(GameCities,how='left',on=['Season','WTeamID','LTeamID'])\n```\nAs a result, the test data was converted from 10080 rows to 10200 rows.  But the training data could be merged without increasing the rows.\nI found that this could increase if the key matched multiple rows in other DataFrames.\n[Reference](https://stackoverflow.com/questions/22720739/pandas-left-outer-join-results-in-table-larger-than-left-table)\nIs there a way to prevent rows from increasing and combine them?",
      "votes": null
    },
    {
      "id": "767464",
      "postDate": "03/09/2020 17:09:35",
      "content": "<p>Yes, probably you are merging with multiple matches, as in the same season, there can be multiple tuples <code>('Season','WTeamID','LTeamID')</code> in your GameCities files (Imagine that both teams encountered each other multiple times in a season including regular/secondary/ncaa). What you should do in my opinion is trying to add <code>'DayNum'</code> in your <code>'on'</code> parameter, because two teams can't meet each other twice in the same day, that will be unique match.</p>",
      "rawMarkdown": "Yes, probably you are merging with multiple matches, as in the same season, there can be multiple tuples `('Season','WTeamID','LTeamID')` in your GameCities files (Imagine that both teams encountered each other multiple times in a season including regular/secondary/ncaa). What you should do in my opinion is trying to add `'DayNum'` in your `'on'` parameter, because two teams can't meet each other twice in the same day, that will be unique match.",
      "votes": null
    },
    {
      "id": "767497",
      "postDate": "03/09/2020 17:59:44",
      "content": "<p>Yes, the combination of Season, DayNum, and WTeamID is sufficient to uniquely identify each game, and so you can add in LTeamID as well if you like.  But it is certainly true that there are multiple games played between the same two teams in the same season.</p>",
      "rawMarkdown": "Yes, the combination of Season, DayNum, and WTeamID is sufficient to uniquely identify each game, and so you can add in LTeamID as well if you like.  But it is certainly true that there are multiple games played between the same two teams in the same season.",
      "votes": null
    },
    {
      "id": "768294",
      "postDate": "03/10/2020 16:00:43",
      "content": "<p><code>\ntest = test.merge(GameCities,how='left',on=['Season','WTeamID','LTeamID','DayNum'])\n</code>\nI was able to prevent rows from increasing!\nThank you!</p>\n\n<p>Only games played between the same two teams during the same season were extracted.\n```\nprint(test[test.duplicated(subset=['Season','WTeamID','LTeamID'])])</p>\n\n<h1>[140 rows x 21 columns]</h1>\n\n<p>```\nI will use this result as well.\nThank you very much!</p>",
      "rawMarkdown": "```\ntest = test.merge(GameCities,how='left',on=['Season','WTeamID','LTeamID','DayNum'])\n```\nI was able to prevent rows from increasing!\nThank you!\n\nOnly games played between the same two teams during the same season were extracted.\n```\nprint(test[test.duplicated(subset=['Season','WTeamID','LTeamID'])])\n#[140 rows x 21 columns]\n```\nI will use this result as well.\nThank you very much!",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 767464,
      "author_name": "jielu0728",
      "author_url": "",
      "post_date": "03/09/2020 17:09:35",
      "content": "<p>Yes, probably you are merging with multiple matches, as in the same season, there can be multiple tuples <code>('Season','WTeamID','LTeamID')</code> in your GameCities files (Imagine that both teams encountered each other multiple times in a season including regular/secondary/ncaa). What you should do in my opinion is trying to add <code>'DayNum'</code> in your <code>'on'</code> parameter, because two teams can't meet each other twice in the same day, that will be unique match.</p>",
      "votes": null,
      "replies": [
        {
          "id": 767497,
          "author_name": "jeffsonas",
          "author_url": "",
          "post_date": "03/09/2020 17:59:44",
          "content": "<p>Yes, the combination of Season, DayNum, and WTeamID is sufficient to uniquely identify each game, and so you can add in LTeamID as well if you like.  But it is certainly true that there are multiple games played between the same two teams in the same season.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 768294,
          "author_name": "mrboupp",
          "author_url": "",
          "post_date": "03/10/2020 16:00:43",
          "content": "<p><code>\ntest = test.merge(GameCities,how='left',on=['Season','WTeamID','LTeamID','DayNum'])\n</code>\nI was able to prevent rows from increasing!\nThank you!</p>\n\n<p>Only games played between the same two teams during the same season were extracted.\n```\nprint(test[test.duplicated(subset=['Season','WTeamID','LTeamID'])])</p>\n\n<h1>[140 rows x 21 columns]</h1>\n\n<p>```\nI will use this result as well.\nThank you very much!</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "767339": "I used Pandas' merge () function to combine test data and game city data.\n```\ntest_df = test_df.merge(GameCities,how='left',on=['Season','WTeamID','LTeamID'])\n```\nAs a result, the test data was converted from 10080 rows to 10200 rows.  But the training data could be merged without increasing the rows.\nI found that this could increase if the key matched multiple rows in other DataFrames.\n[Reference](https://stackoverflow.com/questions/22720739/pandas-left-outer-join-results-in-table-larger-than-left-table)\nIs there a way to prevent rows from increasing and combine them?",
    "767464": "Yes, probably you are merging with multiple matches, as in the same season, there can be multiple tuples `('Season','WTeamID','LTeamID')` in your GameCities files (Imagine that both teams encountered each other multiple times in a season including regular/secondary/ncaa). What you should do in my opinion is trying to add `'DayNum'` in your `'on'` parameter, because two teams can't meet each other twice in the same day, that will be unique match.",
    "767497": "Yes, the combination of Season, DayNum, and WTeamID is sufficient to uniquely identify each game, and so you can add in LTeamID as well if you like.  But it is certainly true that there are multiple games played between the same two teams in the same season.",
    "768294": "```\ntest = test.merge(GameCities,how='left',on=['Season','WTeamID','LTeamID','DayNum'])\n```\nI was able to prevent rows from increasing!\nThank you!\n\nOnly games played between the same two teams during the same season were extracted.\n```\nprint(test[test.duplicated(subset=['Season','WTeamID','LTeamID'])])\n#[140 rows x 21 columns]\n```\nI will use this result as well.\nThank you very much!"
  },
  "source": "meta"
}