Python Count Unique value in Row csv











up vote
2
down vote

favorite
1












I have CSV, which is in a list.
Example:



[[R2C1,R01,API_1,801,API_TEST01],
[R2C1,R01,API_1,802,API_TEST02],
[R2C1,R01,API_1,801,API_TEST03]]


Like to find out the all the unique in i[3] and count them.
results:



[{num: 801, count: 2}, {num: 802, count: 1}]


so that I can call dict key for another test.



Code:



    for row in data[1:]:
vnum = row[3]
ipcount.append({"num":vnum,"count": count})
if row[3] not in ipcount:
ipcount.append({"num":vlan})









share|improve this question




























    up vote
    2
    down vote

    favorite
    1












    I have CSV, which is in a list.
    Example:



    [[R2C1,R01,API_1,801,API_TEST01],
    [R2C1,R01,API_1,802,API_TEST02],
    [R2C1,R01,API_1,801,API_TEST03]]


    Like to find out the all the unique in i[3] and count them.
    results:



    [{num: 801, count: 2}, {num: 802, count: 1}]


    so that I can call dict key for another test.



    Code:



        for row in data[1:]:
    vnum = row[3]
    ipcount.append({"num":vnum,"count": count})
    if row[3] not in ipcount:
    ipcount.append({"num":vlan})









    share|improve this question


























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I have CSV, which is in a list.
      Example:



      [[R2C1,R01,API_1,801,API_TEST01],
      [R2C1,R01,API_1,802,API_TEST02],
      [R2C1,R01,API_1,801,API_TEST03]]


      Like to find out the all the unique in i[3] and count them.
      results:



      [{num: 801, count: 2}, {num: 802, count: 1}]


      so that I can call dict key for another test.



      Code:



          for row in data[1:]:
      vnum = row[3]
      ipcount.append({"num":vnum,"count": count})
      if row[3] not in ipcount:
      ipcount.append({"num":vlan})









      share|improve this question















      I have CSV, which is in a list.
      Example:



      [[R2C1,R01,API_1,801,API_TEST01],
      [R2C1,R01,API_1,802,API_TEST02],
      [R2C1,R01,API_1,801,API_TEST03]]


      Like to find out the all the unique in i[3] and count them.
      results:



      [{num: 801, count: 2}, {num: 802, count: 1}]


      so that I can call dict key for another test.



      Code:



          for row in data[1:]:
      vnum = row[3]
      ipcount.append({"num":vnum,"count": count})
      if row[3] not in ipcount:
      ipcount.append({"num":vlan})






      python csv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 7:36









      Vineeth Sai

      2,28441023




      2,28441023










      asked Nov 9 at 7:34









      miu

      184




      184
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You can do this using a dictionary in order to group list items by num element. The last step is using a list comprehension in order to achieve your desired result.



          dict = {}
          for elem in data:
          if elem[3] not in dict:
          dict[elem[3]] = 0
          dict[elem[3]] = dict[elem[3]] + 1

          final_list = [{'num' : elem, 'count': dict[elem]} for elem in dict]


          Output



          [{'num': 801, 'count': 2}, {'num': 802, 'count': 1}]





          share|improve this answer





















          • how interesting that you using comprehension. Thanks sharing.
            – miu
            Nov 9 at 16:30


















          up vote
          1
          down vote













          If you use the pandas library:



          import pandas as pd
          # Open your file using pd.read_csv() or from your list of lists
          df = pd.DataFrame([['R2C1','R01','API_1',801,'API_TEST01'],
          ['R2C1','R01','API_1',802,'API_TEST02'],
          ['R2C1','R01','API_1',801,'API_TEST03']])
          print(df)
          0 1 2 3 4
          0 R2C1 R01 API_1 801 API_TEST01
          1 R2C1 R01 API_1 802 API_TEST02
          2 R2C1 R01 API_1 801 API_TEST03


          Here you can use .value_counts() to get the number of each value in column 3, then using a dictionary comprehension transform this into the form you need:



          [{'num': k, 'count': v} for k, v in dict(df[3].value_counts()).items()]
          [{'num': 801, 'count': 2}, {'num': 802, 'count': 1}]





          share|improve this answer























          • thanks Alex, i was looking at pandas, using SeriesGroupBy.nunique but was not able to get the results. Thanks
            – miu
            Nov 9 at 16:29










          • If you’ve got a large amount of data to sort through pandas is very quick. If you had any other questions check out the pandas tag/ ask there
            – Alex
            Nov 10 at 17:04




















          up vote
          0
          down vote













          here a pure pandas approach without any loops



          import pandas as pd 

          # define path to data
          PATH = u'pathtodata.csv'

          # create panda datafrmae
          df = pd.read_csv(PATH, usecols = [0,1,2,3], header = 0, names = ['a', 'b', 'c','num'])

          # Add count to column of interest
          df['count'] = df.groupby('num')['num'].transform('count')

          # only keep unique values in column of interest
          df.drop_duplicates(subset=['num'], inplace = True)

          # create dict from bowth columns
          your_output = dict(zip(df.num, df.count))





          share|improve this answer





















          • Thanks sudonym, what does the zip do? does it combine and remove duplicates?
            – miu
            Nov 9 at 16:31











          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',
          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%2f53221498%2fpython-count-unique-value-in-row-csv%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








          up vote
          1
          down vote



          accepted










          You can do this using a dictionary in order to group list items by num element. The last step is using a list comprehension in order to achieve your desired result.



          dict = {}
          for elem in data:
          if elem[3] not in dict:
          dict[elem[3]] = 0
          dict[elem[3]] = dict[elem[3]] + 1

          final_list = [{'num' : elem, 'count': dict[elem]} for elem in dict]


          Output



          [{'num': 801, 'count': 2}, {'num': 802, 'count': 1}]





          share|improve this answer





















          • how interesting that you using comprehension. Thanks sharing.
            – miu
            Nov 9 at 16:30















          up vote
          1
          down vote



          accepted










          You can do this using a dictionary in order to group list items by num element. The last step is using a list comprehension in order to achieve your desired result.



          dict = {}
          for elem in data:
          if elem[3] not in dict:
          dict[elem[3]] = 0
          dict[elem[3]] = dict[elem[3]] + 1

          final_list = [{'num' : elem, 'count': dict[elem]} for elem in dict]


          Output



          [{'num': 801, 'count': 2}, {'num': 802, 'count': 1}]





          share|improve this answer





















          • how interesting that you using comprehension. Thanks sharing.
            – miu
            Nov 9 at 16:30













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You can do this using a dictionary in order to group list items by num element. The last step is using a list comprehension in order to achieve your desired result.



          dict = {}
          for elem in data:
          if elem[3] not in dict:
          dict[elem[3]] = 0
          dict[elem[3]] = dict[elem[3]] + 1

          final_list = [{'num' : elem, 'count': dict[elem]} for elem in dict]


          Output



          [{'num': 801, 'count': 2}, {'num': 802, 'count': 1}]





          share|improve this answer












          You can do this using a dictionary in order to group list items by num element. The last step is using a list comprehension in order to achieve your desired result.



          dict = {}
          for elem in data:
          if elem[3] not in dict:
          dict[elem[3]] = 0
          dict[elem[3]] = dict[elem[3]] + 1

          final_list = [{'num' : elem, 'count': dict[elem]} for elem in dict]


          Output



          [{'num': 801, 'count': 2}, {'num': 802, 'count': 1}]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 7:46









          Mihai Alexandru-Ionut

          29.1k63568




          29.1k63568












          • how interesting that you using comprehension. Thanks sharing.
            – miu
            Nov 9 at 16:30


















          • how interesting that you using comprehension. Thanks sharing.
            – miu
            Nov 9 at 16:30
















          how interesting that you using comprehension. Thanks sharing.
          – miu
          Nov 9 at 16:30




          how interesting that you using comprehension. Thanks sharing.
          – miu
          Nov 9 at 16:30












          up vote
          1
          down vote













          If you use the pandas library:



          import pandas as pd
          # Open your file using pd.read_csv() or from your list of lists
          df = pd.DataFrame([['R2C1','R01','API_1',801,'API_TEST01'],
          ['R2C1','R01','API_1',802,'API_TEST02'],
          ['R2C1','R01','API_1',801,'API_TEST03']])
          print(df)
          0 1 2 3 4
          0 R2C1 R01 API_1 801 API_TEST01
          1 R2C1 R01 API_1 802 API_TEST02
          2 R2C1 R01 API_1 801 API_TEST03


          Here you can use .value_counts() to get the number of each value in column 3, then using a dictionary comprehension transform this into the form you need:



          [{'num': k, 'count': v} for k, v in dict(df[3].value_counts()).items()]
          [{'num': 801, 'count': 2}, {'num': 802, 'count': 1}]





          share|improve this answer























          • thanks Alex, i was looking at pandas, using SeriesGroupBy.nunique but was not able to get the results. Thanks
            – miu
            Nov 9 at 16:29










          • If you’ve got a large amount of data to sort through pandas is very quick. If you had any other questions check out the pandas tag/ ask there
            – Alex
            Nov 10 at 17:04

















          up vote
          1
          down vote













          If you use the pandas library:



          import pandas as pd
          # Open your file using pd.read_csv() or from your list of lists
          df = pd.DataFrame([['R2C1','R01','API_1',801,'API_TEST01'],
          ['R2C1','R01','API_1',802,'API_TEST02'],
          ['R2C1','R01','API_1',801,'API_TEST03']])
          print(df)
          0 1 2 3 4
          0 R2C1 R01 API_1 801 API_TEST01
          1 R2C1 R01 API_1 802 API_TEST02
          2 R2C1 R01 API_1 801 API_TEST03


          Here you can use .value_counts() to get the number of each value in column 3, then using a dictionary comprehension transform this into the form you need:



          [{'num': k, 'count': v} for k, v in dict(df[3].value_counts()).items()]
          [{'num': 801, 'count': 2}, {'num': 802, 'count': 1}]





          share|improve this answer























          • thanks Alex, i was looking at pandas, using SeriesGroupBy.nunique but was not able to get the results. Thanks
            – miu
            Nov 9 at 16:29










          • If you’ve got a large amount of data to sort through pandas is very quick. If you had any other questions check out the pandas tag/ ask there
            – Alex
            Nov 10 at 17:04















          up vote
          1
          down vote










          up vote
          1
          down vote









          If you use the pandas library:



          import pandas as pd
          # Open your file using pd.read_csv() or from your list of lists
          df = pd.DataFrame([['R2C1','R01','API_1',801,'API_TEST01'],
          ['R2C1','R01','API_1',802,'API_TEST02'],
          ['R2C1','R01','API_1',801,'API_TEST03']])
          print(df)
          0 1 2 3 4
          0 R2C1 R01 API_1 801 API_TEST01
          1 R2C1 R01 API_1 802 API_TEST02
          2 R2C1 R01 API_1 801 API_TEST03


          Here you can use .value_counts() to get the number of each value in column 3, then using a dictionary comprehension transform this into the form you need:



          [{'num': k, 'count': v} for k, v in dict(df[3].value_counts()).items()]
          [{'num': 801, 'count': 2}, {'num': 802, 'count': 1}]





          share|improve this answer














          If you use the pandas library:



          import pandas as pd
          # Open your file using pd.read_csv() or from your list of lists
          df = pd.DataFrame([['R2C1','R01','API_1',801,'API_TEST01'],
          ['R2C1','R01','API_1',802,'API_TEST02'],
          ['R2C1','R01','API_1',801,'API_TEST03']])
          print(df)
          0 1 2 3 4
          0 R2C1 R01 API_1 801 API_TEST01
          1 R2C1 R01 API_1 802 API_TEST02
          2 R2C1 R01 API_1 801 API_TEST03


          Here you can use .value_counts() to get the number of each value in column 3, then using a dictionary comprehension transform this into the form you need:



          [{'num': k, 'count': v} for k, v in dict(df[3].value_counts()).items()]
          [{'num': 801, 'count': 2}, {'num': 802, 'count': 1}]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 7:52

























          answered Nov 9 at 7:41









          Alex

          720621




          720621












          • thanks Alex, i was looking at pandas, using SeriesGroupBy.nunique but was not able to get the results. Thanks
            – miu
            Nov 9 at 16:29










          • If you’ve got a large amount of data to sort through pandas is very quick. If you had any other questions check out the pandas tag/ ask there
            – Alex
            Nov 10 at 17:04




















          • thanks Alex, i was looking at pandas, using SeriesGroupBy.nunique but was not able to get the results. Thanks
            – miu
            Nov 9 at 16:29










          • If you’ve got a large amount of data to sort through pandas is very quick. If you had any other questions check out the pandas tag/ ask there
            – Alex
            Nov 10 at 17:04


















          thanks Alex, i was looking at pandas, using SeriesGroupBy.nunique but was not able to get the results. Thanks
          – miu
          Nov 9 at 16:29




          thanks Alex, i was looking at pandas, using SeriesGroupBy.nunique but was not able to get the results. Thanks
          – miu
          Nov 9 at 16:29












          If you’ve got a large amount of data to sort through pandas is very quick. If you had any other questions check out the pandas tag/ ask there
          – Alex
          Nov 10 at 17:04






          If you’ve got a large amount of data to sort through pandas is very quick. If you had any other questions check out the pandas tag/ ask there
          – Alex
          Nov 10 at 17:04












          up vote
          0
          down vote













          here a pure pandas approach without any loops



          import pandas as pd 

          # define path to data
          PATH = u'pathtodata.csv'

          # create panda datafrmae
          df = pd.read_csv(PATH, usecols = [0,1,2,3], header = 0, names = ['a', 'b', 'c','num'])

          # Add count to column of interest
          df['count'] = df.groupby('num')['num'].transform('count')

          # only keep unique values in column of interest
          df.drop_duplicates(subset=['num'], inplace = True)

          # create dict from bowth columns
          your_output = dict(zip(df.num, df.count))





          share|improve this answer





















          • Thanks sudonym, what does the zip do? does it combine and remove duplicates?
            – miu
            Nov 9 at 16:31















          up vote
          0
          down vote













          here a pure pandas approach without any loops



          import pandas as pd 

          # define path to data
          PATH = u'pathtodata.csv'

          # create panda datafrmae
          df = pd.read_csv(PATH, usecols = [0,1,2,3], header = 0, names = ['a', 'b', 'c','num'])

          # Add count to column of interest
          df['count'] = df.groupby('num')['num'].transform('count')

          # only keep unique values in column of interest
          df.drop_duplicates(subset=['num'], inplace = True)

          # create dict from bowth columns
          your_output = dict(zip(df.num, df.count))





          share|improve this answer





















          • Thanks sudonym, what does the zip do? does it combine and remove duplicates?
            – miu
            Nov 9 at 16:31













          up vote
          0
          down vote










          up vote
          0
          down vote









          here a pure pandas approach without any loops



          import pandas as pd 

          # define path to data
          PATH = u'pathtodata.csv'

          # create panda datafrmae
          df = pd.read_csv(PATH, usecols = [0,1,2,3], header = 0, names = ['a', 'b', 'c','num'])

          # Add count to column of interest
          df['count'] = df.groupby('num')['num'].transform('count')

          # only keep unique values in column of interest
          df.drop_duplicates(subset=['num'], inplace = True)

          # create dict from bowth columns
          your_output = dict(zip(df.num, df.count))





          share|improve this answer












          here a pure pandas approach without any loops



          import pandas as pd 

          # define path to data
          PATH = u'pathtodata.csv'

          # create panda datafrmae
          df = pd.read_csv(PATH, usecols = [0,1,2,3], header = 0, names = ['a', 'b', 'c','num'])

          # Add count to column of interest
          df['count'] = df.groupby('num')['num'].transform('count')

          # only keep unique values in column of interest
          df.drop_duplicates(subset=['num'], inplace = True)

          # create dict from bowth columns
          your_output = dict(zip(df.num, df.count))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 8:16









          sudonym

          1,291924




          1,291924












          • Thanks sudonym, what does the zip do? does it combine and remove duplicates?
            – miu
            Nov 9 at 16:31


















          • Thanks sudonym, what does the zip do? does it combine and remove duplicates?
            – miu
            Nov 9 at 16:31
















          Thanks sudonym, what does the zip do? does it combine and remove duplicates?
          – miu
          Nov 9 at 16:31




          Thanks sudonym, what does the zip do? does it combine and remove duplicates?
          – miu
          Nov 9 at 16:31


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53221498%2fpython-count-unique-value-in-row-csv%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







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini