Python Count Unique value in Row csv

Python Count Unique value in Row csv

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})

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}]

python count number of unique elements in csv column, You're looking for the SeriesGroupby method nunique : In [11]: df Out[11]: 0 1 0 AB asd 1 AB poi 2 AB asd 3 BG put 4 BG asd In [12]: g  I'm trying to get the counts of unique items in a csv column using Python. Sample CSV file (has no header): AB,asd AB,poi AB,asd BG,put BG,asd I've tried this so far. import csv from collecti


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}]

Python, Pandas nunique() is used to get a count of unique values. To download the CSV file used, Click Here. Return Type: Integer – Number of unique values in a column. In this example, nunique() method is used to get number of all unique values in Team column. In pandas, for a column in a DataFrame, we can use the value_counts() method to easily count the unique occurences of values. There's additional interesting analyis we can do with value_counts() too. We'll try them out using the titanic dataset.


here a pure pandas approach without any loops

import pandas as pd 

# define path to data
PATH = u'path\to\data.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))

Getting a count of unique values for a single column, csv' accidents = pd.read_csv(accidents_data_file, sep=',', header=0, index_col=​False, parse_dates=['Date'], dayfirst=True, tupleize_cols=False,  I have a .csv file that contains about 70k rows and 23 columns (row count and content will change, but columns are fixed). I'd like to have python read the file (which is working okay), then generate a list of unique values in column 13, and a count of each of those unique items where column 8 has a specific variable.


How to Get Unique Values from a Column in Pandas Data Frame , List Unique Values in Pandas Column. load the data with pd.read_csv. gapminder = pd.read_csv(gapminder_csv_url)  Right now, I have it iterating over and comparing each value in order. If a unique value appears, it only stores the first occurrence in the dictionary. I changed it to now also check if that value has already occurred before, and if so, to skip it.


Count of unique items based on condition, I have a .csv file that contains about 70k rows and 23 columns (row count. of unique values in column 13, and a count of each of those unique  arr: Numpy array in which we want to find the unique values. return_index: optional bool flag. If True returns an array of indices of first occurrence of each unique value. return_counts: optional bool flag. If True returns an array of occurrence count of each unique value. axis: If not provided then will act on flattened array. If 0 or 1 then


40- Pandas DataFrames: Counting and getting Unique Values , Visit my personal web-page for the Python code: http://www.brunel.ac.uk/~​csstnns.Duration: 4:48 Posted: Dec 29, 2016 If we try the unique function on the ‘country’ column from the dataframe, the result will be a big numpy array. >gapminder['country'].unique() Instead, we can simply count the number of unique values in the country column and find that there are 142 countries in the data set. >len(gapminder['country'].unique().tolist()) 142 How To Get Unique Values of a Column with drop_duplicates()