How to count a value with JSON based on another value in the JSON file












1















I have a JSON file made up of an array which contains dictionaries, each dictionary is an opinion of a buyer on a specific garage.
I want to find out how many occurrence of each car's type I have in each garage, it looks like this:



[
{"garage": "mike_gar", "reliability": 6, "car_type": "ford", "time": "16:10:36"},
{"garage": "bill_gar", "reliability": 5,"car_type": "kia", "time": "4:37:22"},
{"garage": "alison_gar", "reliability": 1, "car_type": "kia", "time": "11:25:40"},
{"garage": "alison_gar", "reliability": 10, "car_type": "mazda", "time": "2:18:42"},
{"garage": "mike_gar", "reliability": 3, "car_type": "mazda", "time": "12:14:20"},
{"garage": "mike_gar", "reliability": 2, "car_type": "ford", "time": "2:08:27"}
]


Assuming we already read from the JSON file to a variable g_arr.
I'v tried to use reduce() to count the occurrence , but failed to succeed.



output example: {"garage" : "mike_gar", "types":{"ford" : 2, "mazda": 1}}










share|improve this question




















  • 1





    Is Jason the guy who owns the garage?

    – wim
    Dec 31 '18 at 21:32











  • @wim I think it's supposed to be JSON. Fixed that.

    – Jack Moody
    Dec 31 '18 at 21:37











  • @wim opppsss...

    – Shelly875
    Dec 31 '18 at 21:38











  • @Shelly875 I think you will find this answer helpful. Let us know if that solves your problem. It isn't an exact duplicate, but it is pretty closely related.

    – Jack Moody
    Dec 31 '18 at 21:39
















1















I have a JSON file made up of an array which contains dictionaries, each dictionary is an opinion of a buyer on a specific garage.
I want to find out how many occurrence of each car's type I have in each garage, it looks like this:



[
{"garage": "mike_gar", "reliability": 6, "car_type": "ford", "time": "16:10:36"},
{"garage": "bill_gar", "reliability": 5,"car_type": "kia", "time": "4:37:22"},
{"garage": "alison_gar", "reliability": 1, "car_type": "kia", "time": "11:25:40"},
{"garage": "alison_gar", "reliability": 10, "car_type": "mazda", "time": "2:18:42"},
{"garage": "mike_gar", "reliability": 3, "car_type": "mazda", "time": "12:14:20"},
{"garage": "mike_gar", "reliability": 2, "car_type": "ford", "time": "2:08:27"}
]


Assuming we already read from the JSON file to a variable g_arr.
I'v tried to use reduce() to count the occurrence , but failed to succeed.



output example: {"garage" : "mike_gar", "types":{"ford" : 2, "mazda": 1}}










share|improve this question




















  • 1





    Is Jason the guy who owns the garage?

    – wim
    Dec 31 '18 at 21:32











  • @wim I think it's supposed to be JSON. Fixed that.

    – Jack Moody
    Dec 31 '18 at 21:37











  • @wim opppsss...

    – Shelly875
    Dec 31 '18 at 21:38











  • @Shelly875 I think you will find this answer helpful. Let us know if that solves your problem. It isn't an exact duplicate, but it is pretty closely related.

    – Jack Moody
    Dec 31 '18 at 21:39














1












1








1








I have a JSON file made up of an array which contains dictionaries, each dictionary is an opinion of a buyer on a specific garage.
I want to find out how many occurrence of each car's type I have in each garage, it looks like this:



[
{"garage": "mike_gar", "reliability": 6, "car_type": "ford", "time": "16:10:36"},
{"garage": "bill_gar", "reliability": 5,"car_type": "kia", "time": "4:37:22"},
{"garage": "alison_gar", "reliability": 1, "car_type": "kia", "time": "11:25:40"},
{"garage": "alison_gar", "reliability": 10, "car_type": "mazda", "time": "2:18:42"},
{"garage": "mike_gar", "reliability": 3, "car_type": "mazda", "time": "12:14:20"},
{"garage": "mike_gar", "reliability": 2, "car_type": "ford", "time": "2:08:27"}
]


Assuming we already read from the JSON file to a variable g_arr.
I'v tried to use reduce() to count the occurrence , but failed to succeed.



output example: {"garage" : "mike_gar", "types":{"ford" : 2, "mazda": 1}}










share|improve this question
















I have a JSON file made up of an array which contains dictionaries, each dictionary is an opinion of a buyer on a specific garage.
I want to find out how many occurrence of each car's type I have in each garage, it looks like this:



[
{"garage": "mike_gar", "reliability": 6, "car_type": "ford", "time": "16:10:36"},
{"garage": "bill_gar", "reliability": 5,"car_type": "kia", "time": "4:37:22"},
{"garage": "alison_gar", "reliability": 1, "car_type": "kia", "time": "11:25:40"},
{"garage": "alison_gar", "reliability": 10, "car_type": "mazda", "time": "2:18:42"},
{"garage": "mike_gar", "reliability": 3, "car_type": "mazda", "time": "12:14:20"},
{"garage": "mike_gar", "reliability": 2, "car_type": "ford", "time": "2:08:27"}
]


Assuming we already read from the JSON file to a variable g_arr.
I'v tried to use reduce() to count the occurrence , but failed to succeed.



output example: {"garage" : "mike_gar", "types":{"ford" : 2, "mazda": 1}}







python json dictionary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 21:37









Jack Moody

7481723




7481723










asked Dec 31 '18 at 21:26









Shelly875Shelly875

376




376








  • 1





    Is Jason the guy who owns the garage?

    – wim
    Dec 31 '18 at 21:32











  • @wim I think it's supposed to be JSON. Fixed that.

    – Jack Moody
    Dec 31 '18 at 21:37











  • @wim opppsss...

    – Shelly875
    Dec 31 '18 at 21:38











  • @Shelly875 I think you will find this answer helpful. Let us know if that solves your problem. It isn't an exact duplicate, but it is pretty closely related.

    – Jack Moody
    Dec 31 '18 at 21:39














  • 1





    Is Jason the guy who owns the garage?

    – wim
    Dec 31 '18 at 21:32











  • @wim I think it's supposed to be JSON. Fixed that.

    – Jack Moody
    Dec 31 '18 at 21:37











  • @wim opppsss...

    – Shelly875
    Dec 31 '18 at 21:38











  • @Shelly875 I think you will find this answer helpful. Let us know if that solves your problem. It isn't an exact duplicate, but it is pretty closely related.

    – Jack Moody
    Dec 31 '18 at 21:39








1




1





Is Jason the guy who owns the garage?

– wim
Dec 31 '18 at 21:32





Is Jason the guy who owns the garage?

– wim
Dec 31 '18 at 21:32













@wim I think it's supposed to be JSON. Fixed that.

– Jack Moody
Dec 31 '18 at 21:37





@wim I think it's supposed to be JSON. Fixed that.

– Jack Moody
Dec 31 '18 at 21:37













@wim opppsss...

– Shelly875
Dec 31 '18 at 21:38





@wim opppsss...

– Shelly875
Dec 31 '18 at 21:38













@Shelly875 I think you will find this answer helpful. Let us know if that solves your problem. It isn't an exact duplicate, but it is pretty closely related.

– Jack Moody
Dec 31 '18 at 21:39





@Shelly875 I think you will find this answer helpful. Let us know if that solves your problem. It isn't an exact duplicate, but it is pretty closely related.

– Jack Moody
Dec 31 '18 at 21:39












3 Answers
3






active

oldest

votes


















1














Here's a solution based on reduction. First, I test whether the garage exists in the accumulation dictionary, and if not, create it. Then, I check whether the car type exists in the garage dictionary, and if not, I create it. Finally, I increment the car type.



res = {}

for d in garages:
if d["garage"] not in res:
res[d["garage"]] = {"garage": d["garage"], "types": {}}

if d["car_type"] not in res[d["garage"]]["types"]:
res[d["garage"]]["types"][d["car_type"]] = 0

res[d["garage"]]["types"][d["car_type"]] += 1


Output:



{
'mike_gar': {'garage': 'mike_gar', 'types': {'ford': 2, 'mazda': 1}},
'bill_gar': {'garage': 'bill_gar', 'types': {'kia': 1}},
'alison_gar': {'garage': 'alison_gar', 'types': {'kia': 1, 'mazda': 1}}
}


Try it!



If you'd like your result in an array, use res.values().






share|improve this answer


























  • That is a Javascript solution, the question has a Python tag.

    – amanb
    Dec 31 '18 at 21:55











  • Whoops--coded before looking! Changed to Python.

    – ggorlen
    Dec 31 '18 at 22:03






  • 1





    I'v tried the code and that's exactly what I was looking for! thank you :)

    – Shelly875
    Jan 1 at 9:21



















1














You can simply parse your data and do the count as the following:



garages = 
cars =
output =

for element in data:
if element['garage'] not in garages: garages.append(element['garage'])
if element['car_type'] not in cars: cars.append(element['car_type'])

for type in garages:
current = {}
current['types'] = {}
current['garage'] = type
for element in data:
if element['car_type'] not in current['types']:
current['types'][element['car_type']]=0

if current['garage'] == element['garage']:
for car_type in cars:
if element['car_type'] == car_type:
current['types'][element['car_type']]+=1
output.append(current)

print output


the output of executing the above is:



[{'garage': 'mike_gar', 'types': {'mazda': 1, 'kia': 0, 'ford': 2}}, {'garage': 'bill_gar', 'types': {'mazda': 0, 'kia': 1, 'ford': 0}}, {'garage': 'alison_gar', 'types': {'mazda': 1, 'kia': 1, 'ford': 0}}]





share|improve this answer































    1














    Pandas package is great for working with a such data. You can easily convert your list into a Pandas dataframe.



    import pandas as pd

    df = pd.DataFrame(g_arr)
    print(df)


    Prints:



      car_type      garage  reliability      time
    0 ford mike_gar 6 16:10:36
    1 kia bill_gar 5 4:37:22
    2 kia alison_gar 1 11:25:40
    3 mazda alison_gar 10 2:18:42
    4 mazda mike_gar 3 12:14:20
    5 ford mike_gar 2 2:08:27


    Than you can use the .groupby() method to group your data and the .size() method to get row counts per group.



    print(df.groupby(['garage', 'car_type']).size())


    Prints:



    garage      car_type
    alison_gar kia 1
    mazda 1
    bill_gar kia 1
    mike_gar ford 2
    mazda 1
    dtype: int64





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991537%2fhow-to-count-a-value-with-json-based-on-another-value-in-the-json-file%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Here's a solution based on reduction. First, I test whether the garage exists in the accumulation dictionary, and if not, create it. Then, I check whether the car type exists in the garage dictionary, and if not, I create it. Finally, I increment the car type.



      res = {}

      for d in garages:
      if d["garage"] not in res:
      res[d["garage"]] = {"garage": d["garage"], "types": {}}

      if d["car_type"] not in res[d["garage"]]["types"]:
      res[d["garage"]]["types"][d["car_type"]] = 0

      res[d["garage"]]["types"][d["car_type"]] += 1


      Output:



      {
      'mike_gar': {'garage': 'mike_gar', 'types': {'ford': 2, 'mazda': 1}},
      'bill_gar': {'garage': 'bill_gar', 'types': {'kia': 1}},
      'alison_gar': {'garage': 'alison_gar', 'types': {'kia': 1, 'mazda': 1}}
      }


      Try it!



      If you'd like your result in an array, use res.values().






      share|improve this answer


























      • That is a Javascript solution, the question has a Python tag.

        – amanb
        Dec 31 '18 at 21:55











      • Whoops--coded before looking! Changed to Python.

        – ggorlen
        Dec 31 '18 at 22:03






      • 1





        I'v tried the code and that's exactly what I was looking for! thank you :)

        – Shelly875
        Jan 1 at 9:21
















      1














      Here's a solution based on reduction. First, I test whether the garage exists in the accumulation dictionary, and if not, create it. Then, I check whether the car type exists in the garage dictionary, and if not, I create it. Finally, I increment the car type.



      res = {}

      for d in garages:
      if d["garage"] not in res:
      res[d["garage"]] = {"garage": d["garage"], "types": {}}

      if d["car_type"] not in res[d["garage"]]["types"]:
      res[d["garage"]]["types"][d["car_type"]] = 0

      res[d["garage"]]["types"][d["car_type"]] += 1


      Output:



      {
      'mike_gar': {'garage': 'mike_gar', 'types': {'ford': 2, 'mazda': 1}},
      'bill_gar': {'garage': 'bill_gar', 'types': {'kia': 1}},
      'alison_gar': {'garage': 'alison_gar', 'types': {'kia': 1, 'mazda': 1}}
      }


      Try it!



      If you'd like your result in an array, use res.values().






      share|improve this answer


























      • That is a Javascript solution, the question has a Python tag.

        – amanb
        Dec 31 '18 at 21:55











      • Whoops--coded before looking! Changed to Python.

        – ggorlen
        Dec 31 '18 at 22:03






      • 1





        I'v tried the code and that's exactly what I was looking for! thank you :)

        – Shelly875
        Jan 1 at 9:21














      1












      1








      1







      Here's a solution based on reduction. First, I test whether the garage exists in the accumulation dictionary, and if not, create it. Then, I check whether the car type exists in the garage dictionary, and if not, I create it. Finally, I increment the car type.



      res = {}

      for d in garages:
      if d["garage"] not in res:
      res[d["garage"]] = {"garage": d["garage"], "types": {}}

      if d["car_type"] not in res[d["garage"]]["types"]:
      res[d["garage"]]["types"][d["car_type"]] = 0

      res[d["garage"]]["types"][d["car_type"]] += 1


      Output:



      {
      'mike_gar': {'garage': 'mike_gar', 'types': {'ford': 2, 'mazda': 1}},
      'bill_gar': {'garage': 'bill_gar', 'types': {'kia': 1}},
      'alison_gar': {'garage': 'alison_gar', 'types': {'kia': 1, 'mazda': 1}}
      }


      Try it!



      If you'd like your result in an array, use res.values().






      share|improve this answer















      Here's a solution based on reduction. First, I test whether the garage exists in the accumulation dictionary, and if not, create it. Then, I check whether the car type exists in the garage dictionary, and if not, I create it. Finally, I increment the car type.



      res = {}

      for d in garages:
      if d["garage"] not in res:
      res[d["garage"]] = {"garage": d["garage"], "types": {}}

      if d["car_type"] not in res[d["garage"]]["types"]:
      res[d["garage"]]["types"][d["car_type"]] = 0

      res[d["garage"]]["types"][d["car_type"]] += 1


      Output:



      {
      'mike_gar': {'garage': 'mike_gar', 'types': {'ford': 2, 'mazda': 1}},
      'bill_gar': {'garage': 'bill_gar', 'types': {'kia': 1}},
      'alison_gar': {'garage': 'alison_gar', 'types': {'kia': 1, 'mazda': 1}}
      }


      Try it!



      If you'd like your result in an array, use res.values().







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 31 '18 at 22:02

























      answered Dec 31 '18 at 21:51









      ggorlenggorlen

      7,3883826




      7,3883826













      • That is a Javascript solution, the question has a Python tag.

        – amanb
        Dec 31 '18 at 21:55











      • Whoops--coded before looking! Changed to Python.

        – ggorlen
        Dec 31 '18 at 22:03






      • 1





        I'v tried the code and that's exactly what I was looking for! thank you :)

        – Shelly875
        Jan 1 at 9:21



















      • That is a Javascript solution, the question has a Python tag.

        – amanb
        Dec 31 '18 at 21:55











      • Whoops--coded before looking! Changed to Python.

        – ggorlen
        Dec 31 '18 at 22:03






      • 1





        I'v tried the code and that's exactly what I was looking for! thank you :)

        – Shelly875
        Jan 1 at 9:21

















      That is a Javascript solution, the question has a Python tag.

      – amanb
      Dec 31 '18 at 21:55





      That is a Javascript solution, the question has a Python tag.

      – amanb
      Dec 31 '18 at 21:55













      Whoops--coded before looking! Changed to Python.

      – ggorlen
      Dec 31 '18 at 22:03





      Whoops--coded before looking! Changed to Python.

      – ggorlen
      Dec 31 '18 at 22:03




      1




      1





      I'v tried the code and that's exactly what I was looking for! thank you :)

      – Shelly875
      Jan 1 at 9:21





      I'v tried the code and that's exactly what I was looking for! thank you :)

      – Shelly875
      Jan 1 at 9:21













      1














      You can simply parse your data and do the count as the following:



      garages = 
      cars =
      output =

      for element in data:
      if element['garage'] not in garages: garages.append(element['garage'])
      if element['car_type'] not in cars: cars.append(element['car_type'])

      for type in garages:
      current = {}
      current['types'] = {}
      current['garage'] = type
      for element in data:
      if element['car_type'] not in current['types']:
      current['types'][element['car_type']]=0

      if current['garage'] == element['garage']:
      for car_type in cars:
      if element['car_type'] == car_type:
      current['types'][element['car_type']]+=1
      output.append(current)

      print output


      the output of executing the above is:



      [{'garage': 'mike_gar', 'types': {'mazda': 1, 'kia': 0, 'ford': 2}}, {'garage': 'bill_gar', 'types': {'mazda': 0, 'kia': 1, 'ford': 0}}, {'garage': 'alison_gar', 'types': {'mazda': 1, 'kia': 1, 'ford': 0}}]





      share|improve this answer




























        1














        You can simply parse your data and do the count as the following:



        garages = 
        cars =
        output =

        for element in data:
        if element['garage'] not in garages: garages.append(element['garage'])
        if element['car_type'] not in cars: cars.append(element['car_type'])

        for type in garages:
        current = {}
        current['types'] = {}
        current['garage'] = type
        for element in data:
        if element['car_type'] not in current['types']:
        current['types'][element['car_type']]=0

        if current['garage'] == element['garage']:
        for car_type in cars:
        if element['car_type'] == car_type:
        current['types'][element['car_type']]+=1
        output.append(current)

        print output


        the output of executing the above is:



        [{'garage': 'mike_gar', 'types': {'mazda': 1, 'kia': 0, 'ford': 2}}, {'garage': 'bill_gar', 'types': {'mazda': 0, 'kia': 1, 'ford': 0}}, {'garage': 'alison_gar', 'types': {'mazda': 1, 'kia': 1, 'ford': 0}}]





        share|improve this answer


























          1












          1








          1







          You can simply parse your data and do the count as the following:



          garages = 
          cars =
          output =

          for element in data:
          if element['garage'] not in garages: garages.append(element['garage'])
          if element['car_type'] not in cars: cars.append(element['car_type'])

          for type in garages:
          current = {}
          current['types'] = {}
          current['garage'] = type
          for element in data:
          if element['car_type'] not in current['types']:
          current['types'][element['car_type']]=0

          if current['garage'] == element['garage']:
          for car_type in cars:
          if element['car_type'] == car_type:
          current['types'][element['car_type']]+=1
          output.append(current)

          print output


          the output of executing the above is:



          [{'garage': 'mike_gar', 'types': {'mazda': 1, 'kia': 0, 'ford': 2}}, {'garage': 'bill_gar', 'types': {'mazda': 0, 'kia': 1, 'ford': 0}}, {'garage': 'alison_gar', 'types': {'mazda': 1, 'kia': 1, 'ford': 0}}]





          share|improve this answer













          You can simply parse your data and do the count as the following:



          garages = 
          cars =
          output =

          for element in data:
          if element['garage'] not in garages: garages.append(element['garage'])
          if element['car_type'] not in cars: cars.append(element['car_type'])

          for type in garages:
          current = {}
          current['types'] = {}
          current['garage'] = type
          for element in data:
          if element['car_type'] not in current['types']:
          current['types'][element['car_type']]=0

          if current['garage'] == element['garage']:
          for car_type in cars:
          if element['car_type'] == car_type:
          current['types'][element['car_type']]+=1
          output.append(current)

          print output


          the output of executing the above is:



          [{'garage': 'mike_gar', 'types': {'mazda': 1, 'kia': 0, 'ford': 2}}, {'garage': 'bill_gar', 'types': {'mazda': 0, 'kia': 1, 'ford': 0}}, {'garage': 'alison_gar', 'types': {'mazda': 1, 'kia': 1, 'ford': 0}}]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 31 '18 at 21:57









          Walid Da.Walid Da.

          6041413




          6041413























              1














              Pandas package is great for working with a such data. You can easily convert your list into a Pandas dataframe.



              import pandas as pd

              df = pd.DataFrame(g_arr)
              print(df)


              Prints:



                car_type      garage  reliability      time
              0 ford mike_gar 6 16:10:36
              1 kia bill_gar 5 4:37:22
              2 kia alison_gar 1 11:25:40
              3 mazda alison_gar 10 2:18:42
              4 mazda mike_gar 3 12:14:20
              5 ford mike_gar 2 2:08:27


              Than you can use the .groupby() method to group your data and the .size() method to get row counts per group.



              print(df.groupby(['garage', 'car_type']).size())


              Prints:



              garage      car_type
              alison_gar kia 1
              mazda 1
              bill_gar kia 1
              mike_gar ford 2
              mazda 1
              dtype: int64





              share|improve this answer






























                1














                Pandas package is great for working with a such data. You can easily convert your list into a Pandas dataframe.



                import pandas as pd

                df = pd.DataFrame(g_arr)
                print(df)


                Prints:



                  car_type      garage  reliability      time
                0 ford mike_gar 6 16:10:36
                1 kia bill_gar 5 4:37:22
                2 kia alison_gar 1 11:25:40
                3 mazda alison_gar 10 2:18:42
                4 mazda mike_gar 3 12:14:20
                5 ford mike_gar 2 2:08:27


                Than you can use the .groupby() method to group your data and the .size() method to get row counts per group.



                print(df.groupby(['garage', 'car_type']).size())


                Prints:



                garage      car_type
                alison_gar kia 1
                mazda 1
                bill_gar kia 1
                mike_gar ford 2
                mazda 1
                dtype: int64





                share|improve this answer




























                  1












                  1








                  1







                  Pandas package is great for working with a such data. You can easily convert your list into a Pandas dataframe.



                  import pandas as pd

                  df = pd.DataFrame(g_arr)
                  print(df)


                  Prints:



                    car_type      garage  reliability      time
                  0 ford mike_gar 6 16:10:36
                  1 kia bill_gar 5 4:37:22
                  2 kia alison_gar 1 11:25:40
                  3 mazda alison_gar 10 2:18:42
                  4 mazda mike_gar 3 12:14:20
                  5 ford mike_gar 2 2:08:27


                  Than you can use the .groupby() method to group your data and the .size() method to get row counts per group.



                  print(df.groupby(['garage', 'car_type']).size())


                  Prints:



                  garage      car_type
                  alison_gar kia 1
                  mazda 1
                  bill_gar kia 1
                  mike_gar ford 2
                  mazda 1
                  dtype: int64





                  share|improve this answer















                  Pandas package is great for working with a such data. You can easily convert your list into a Pandas dataframe.



                  import pandas as pd

                  df = pd.DataFrame(g_arr)
                  print(df)


                  Prints:



                    car_type      garage  reliability      time
                  0 ford mike_gar 6 16:10:36
                  1 kia bill_gar 5 4:37:22
                  2 kia alison_gar 1 11:25:40
                  3 mazda alison_gar 10 2:18:42
                  4 mazda mike_gar 3 12:14:20
                  5 ford mike_gar 2 2:08:27


                  Than you can use the .groupby() method to group your data and the .size() method to get row counts per group.



                  print(df.groupby(['garage', 'car_type']).size())


                  Prints:



                  garage      car_type
                  alison_gar kia 1
                  mazda 1
                  bill_gar kia 1
                  mike_gar ford 2
                  mazda 1
                  dtype: int64






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 2 at 22:33

























                  answered Jan 1 at 14:17









                  Mykola ZotkoMykola Zotko

                  1,539416




                  1,539416






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991537%2fhow-to-count-a-value-with-json-based-on-another-value-in-the-json-file%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                      ts Property 'filter' does not exist on type '{}'

                      mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window