{
  "id": 71059,
  "title": "Flatten the 'hit' column",
  "url": "/competitions/ga-customer-revenue-prediction/discussion/71059",
  "author_name": "",
  "post_date": "2018-11-09T19:18:06.667811700Z",
  "votes": 1,
  "comment_count": 3,
  "views": 0,
  "content": "<p>Hello all ! </p>\n\n<p>I am trying to flatten the hit column but i'm stuck.. </p>\n\n<p>I'm trying this : </p>\n\n<p>s = df['hits'].dropna().apply(ast.literal_eval)\npd.concat([pd.DataFrame(x) for x in s], keys=s.index)</p>\n\n<p>But I get this result..  </p>\n\n<p><img src=\"https://i.imgur.com/x2pxxgM.png\" alt=\"Pic\"></p>\n\n<p>Any ideas ?</p>",
  "messages": [
    {
      "id": "418376",
      "postDate": "11/09/2018 19:18:06",
      "content": "<p>Hello all ! </p>\n\n<p>I am trying to flatten the hit column but i'm stuck.. </p>\n\n<p>I'm trying this : </p>\n\n<p>s = df['hits'].dropna().apply(ast.literal_eval)\npd.concat([pd.DataFrame(x) for x in s], keys=s.index)</p>\n\n<p>But I get this result..  </p>\n\n<p><img src=\"https://i.imgur.com/x2pxxgM.png\" alt=\"Pic\"></p>\n\n<p>Any ideas ?</p>",
      "rawMarkdown": "Hello all ! \n\nI am trying to flatten the hit column but i'm stuck.. \n\nI'm trying this : \n\ns = df['hits'].dropna().apply(ast.literal_eval)\npd.concat([pd.DataFrame(x) for x in s], keys=s.index)\n\nBut I get this result..  \n\n![Pic][1]\n\n\n  [1]: https://i.imgur.com/x2pxxgM.png\n\nAny ideas ?",
      "votes": null
    },
    {
      "id": "418602",
      "postDate": "11/10/2018 08:22:26",
      "content": "<p>maybe, this code works well.</p>\n\n<p>ds1 = train_v['hits'].apply(lambda x:pd.DataFrame(x))\npd.concat([ x if len(x) == 1 else x.loc[1].to_frame().T  for x in ds1])</p>",
      "rawMarkdown": "maybe, this code works well.\n\nds1 = train_v['hits'].apply(lambda x:pd.DataFrame(x))\npd.concat([ x if len(x) == 1 else x.loc[1].to_frame().T  for x in ds1])",
      "votes": null
    },
    {
      "id": "418605",
      "postDate": "11/10/2018 08:44:09",
      "content": "<p>train_v['hits'] = train_v['hits'].apply(lambda x:eval(x))</p>",
      "rawMarkdown": "train_v['hits'] = train_v['hits'].apply(lambda x:eval(x))",
      "votes": null
    },
    {
      "id": "418631",
      "postDate": "11/10/2018 10:06:16",
      "content": "<p>Hello Mtgarden,\nThanks for trying to help me out, unfortunately, it did not work with this script. I think \"hits\" is a multiple level JSON.. but very difficult to flatten properly. </p>",
      "rawMarkdown": "Hello Mtgarden,\nThanks for trying to help me out, unfortunately, it did not work with this script. I think \"hits\" is a multiple level JSON.. but very difficult to flatten properly.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 418602,
      "author_name": "achtungbaby",
      "author_url": "",
      "post_date": "11/10/2018 08:22:26",
      "content": "<p>maybe, this code works well.</p>\n\n<p>ds1 = train_v['hits'].apply(lambda x:pd.DataFrame(x))\npd.concat([ x if len(x) == 1 else x.loc[1].to_frame().T  for x in ds1])</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 418605,
      "author_name": "achtungbaby",
      "author_url": "",
      "post_date": "11/10/2018 08:44:09",
      "content": "<p>train_v['hits'] = train_v['hits'].apply(lambda x:eval(x))</p>",
      "votes": null,
      "replies": [
        {
          "id": 418631,
          "author_name": "xtelima",
          "author_url": "",
          "post_date": "11/10/2018 10:06:16",
          "content": "<p>Hello Mtgarden,\nThanks for trying to help me out, unfortunately, it did not work with this script. I think \"hits\" is a multiple level JSON.. but very difficult to flatten properly. </p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "418376": "Hello all ! \n\nI am trying to flatten the hit column but i'm stuck.. \n\nI'm trying this : \n\ns = df['hits'].dropna().apply(ast.literal_eval)\npd.concat([pd.DataFrame(x) for x in s], keys=s.index)\n\nBut I get this result..  \n\n![Pic][1]\n\n\n  [1]: https://i.imgur.com/x2pxxgM.png\n\nAny ideas ?",
    "418602": "maybe, this code works well.\n\nds1 = train_v['hits'].apply(lambda x:pd.DataFrame(x))\npd.concat([ x if len(x) == 1 else x.loc[1].to_frame().T  for x in ds1])",
    "418605": "train_v['hits'] = train_v['hits'].apply(lambda x:eval(x))",
    "418631": "Hello Mtgarden,\nThanks for trying to help me out, unfortunately, it did not work with this script. I think \"hits\" is a multiple level JSON.. but very difficult to flatten properly."
  },
  "source": "meta"
}