title: Manipulating data of a dataframe in Pandas
body: <p>I'm reading a dataframe and converting it into a json file. I'm using python 3 and 0.25.3 version of pandas for it. I already got some help from you guys (<a href="https://stackoverflow.com/questions/59268800/manipulating-data-of-pandas-dataframe">Manipulating data of Pandas dataframe</a>), but I have some questions about the code and how it works. </p>
  
  <p><strong>My dataframe:</strong></p>
  
  <pre><code>id     label        id_customer     label_customer    part_number   number_client
  
  6     Sao Paulo      CUST-99992         Brazil          7897           982
  
  6     Sao Paulo      CUST-99992         Brazil          888            12
  
  92    Hong Kong      CUST-88888         China           147            288
  </code></pre>
  
  <p><strong>Code:</strong> </p>
  
  <pre><code>import pandas as pd
  
  data = pd.read_excel(path)
  
  data[["part_number","number_client"]] = data[["part_number","number_client"]].astype(str)
  
  f = lambda x: x.split('_')[0]
  
  j =(data.groupby(["id","label","id_customer","label_customer"])['part_number','number_client']
          .apply(lambda x: x.rename(columns=f).to_dict('r')).reset_index(name='Number')
          .groupby(["id", "label"])[ "id_customer", "label_customer", "Number"]
          .apply(lambda x: x.rename(columns=f).to_dict('r')).reset_index(name='Customer')
          .to_json(orient='records'))
  
  print (j)
  </code></pre>
  
  <p><strong>Json I'm getting:</strong></p>
  
  <pre><code>[{
          "id": 6,
          "label": "Sao Paulo",
          "Customer": [{
              "id": "CUST-99992",
              "label": "Brazil",
              "number": [{
                      "part": "7897",
                      "client": "982"
                  },
                  {
                      "part": "888",
                      "client": "12"
                  }
              ]
          }]
      },
      {
          "id": 92,
          "label": "Hong Kong",
          "Customer": [{
              "id": "CUST-888888",
              "label": "China",
              "number": [{
                  "part": "147",
                  "client": "288"
              }]
          }]
      }
  ]
  </code></pre>
  
  <p><strong>1st Question:</strong>  <code>lambda</code> and <code>apply</code> function are spliting my columns' name when a <code>_</code> is found.. That is just a piece of my dataframe and some columns I'd like to preserve the name.. e.g: I want get <code>part_number</code> and <code>number_client</code> instead <code>part</code> and <code>client</code> in my json structure. How can I fix this?</p>
  
  <p><strong>2nd Question:</strong> I can have different lists with the same key name. E.g: In <code>customer</code> list I have <code>part_number</code> key, but I can also have the same name of key inside another list with another value. E.g: <code>part_number</code> inside <code>test</code> list. </p>
  
  <p><strong>3rd Question:</strong> In my complete dataframe, I have a column called <code>Additional_information</code> when I have a simple text. I have to get a structure like this:</p>
  
  <pre><code>...
  
  "Additional_information":[{
          {
            "text": "testing",
          }
          },
          {
           "text": "testing again",
          }
          ]
  </code></pre>
  
  <p>for a dataframe like this:</p>
  
  <pre><code>id     label        id_customer     label_customer    part_number   number_client    Additional_information
  
  6     Sao Paulo      CUST-99992         Brazil          7897           982           testing
  
  6     Sao Paulo      CUST-99992         Brazil          7897           982           testing again
  </code></pre>
  
  <p>What should I change?</p>