How to count unique values in pandas column base on dictionary values

rra

I have the below pandas data frame.

d = {'id1': ['85643', '85644','85643','8564312','8564314','85645','8564316','85646','8564318','85647','85648','85649','85655','56731','34566','78931','78931'],'ID': ['G-00001', 'G-00001','G-00002','G-00002','G-00002','G-00001','G-00001','G-00001','G-00001','G-00001','G-00002','G-00002','G-00002','G-00002','G-00003','G-00003','G-00003'],'col1': [671, 2,5,3,4,5,60,0,0,6,3,2,4,32,3,1,23],'Goal': [np.nan, 56,78,np.nan,89,73,np.nan ,np.nan ,np.nan, np.nan, np.nan, 34,np.nan, 7, 84,np.nan,5 ], 'col2': [793, 4,8,32,43,55,610,0,0,16,23,72,48,3,28,5,3],'col3': [500, 22,89,33,44,55,60,1,5,6,3,2,4,13,12,14,98],'Date': ['2021-06-13', '2021-06-13','2021-06-14','2021-06-13','2021-06-14','2021-06-15','2021-06-15','2021-06-13','2021-06-16','2021-06-13','2021-06-13','2021-06-13','2021-06-16','2021-05-23','2021-05-13','2021-03-26','2021-05-13']}
dff = pd.DataFrame(data=d)
dff

    id1     ID      col1    Goal        col2    col3    Date
0   85643   G-00001 671     NaN         793     500 2021-06-13
1   85644   G-00001 2       56.0000     4       22  2021-06-13
2   85643   G-00002 5       78.0000     8       89  2021-06-14
3   8564312 G-00002 3       NaN         32      33  2021-06-13
4   8564314 G-00002 4       89.0000     43      44  2021-06-14
5   85645   G-00001 5       73.0000     55      55  2021-06-15
6   8564316 G-00001 60      NaN         610     60  2021-06-15
7   85646   G-00001 0       NaN         0       1   2021-06-13
8   8564318 G-00001 0       NaN         0       5   2021-06-16
9   85647   G-00001 6       NaN         16      6   2021-06-13
10  85648   G-00002 3       NaN         23      3   2021-06-13
11  85649   G-00002 2       34.0000     72      2   2021-06-13
12  85655   G-00002 4       NaN         48      4   2021-06-16
13  56731   G-00002 32      7.0000      3       13  2021-05-23
14  34566   G-00003 3       84.0000     28      12  2021-05-13
15  78931   G-00003 1       NaN         5       14  2021-03-26
16  78931   G-00003 23      5.0000      3       98  2021-05-13

Also, I have the below dictionary

dic = {'G-00001':{'aasd':['G-00001','85646','85648','345_2','85655','85659','85647'],
'vaasd':['G-00001','85649','34554','85655','22183','45335','8564316']},
'G-00002':{'aasd2':['G-00002','85343','85649','85655','78931','45121','56731']},
'G-00003':{'gsd3':['G-00003','34566','8564312','45121','78931']}}

I want to get a unique count of id1, based on the list inside the dictionary based on theire ID column. For example, if we consider ID - 'G-0002' has only one list aasd2. 'aasd2':['G-0002','85343','85649','85655','78931','45121','56731']. I want to get how many unique id1's are in the 'G-0002' ID column in pandas' data frame. So it should be for aasd2- 3 values ('85649','85655','56731'). Those are the only three id1 values in pandas for aasd2 in 'G-0002' ID.

So I want to create a table just like below for the list name and count of id1s

listName    count of id1s
aasd            2
vaasd           1
aasd2           3
gsd3            2

or is it possible to get kind a below out put

#Is it possible to get what id1 in each list? for example aasd2-value count 3 
#and ['85649','85655','56731'].
ID            id2     Value count        list
G-00002     aasd2     3                 ['85649','85655','56731']
G-00001     aasd      2                 ['85646','85647']
G-00003     gsd3      2                 ['34566','78931']
G-00001     vaasd     1                 ['8564316']

Is it possible to do this in python? Any suggestion would be appreciated. Thanks in advance!

Corralien

From my previous answer, I slightly modified the code:

data = []
for g, d in dic.items():
    for k, l in d.items():
        data.extend([(g, v, k) for v in l])
df1 = pd.DataFrame(data, columns=['ID', 'id1', 'id2'])

out = dff.merge(df1, on=['ID', 'id1']) \
         .drop_duplicates(['ID', 'id1']) \
         .value_counts('id2')
print(out)

# Output:
id2
aasd2    3
aasd     2
gsd3     2
vaasd    1
dtype: int64

Update

Is it possible to get what id1 in each list? for example aasd2-value count 3 and ['85649','85655','56731'].

out = (
  dff.merge(df1, on=['ID', 'id1']).drop_duplicates(['ID', 'id1'])
     .groupby(['ID', 'id2'])
     .agg(**{'Value Count': ('id2', 'size'), 'List id1': ('id1', list)})
     .reset_index()
)
print(out)

# Output:
        ID    id2  Value Count               List id1
0  G-00001   aasd            2         [85646, 85647]
1  G-00001  vaasd            1              [8564316]
2  G-00002  aasd2            3  [85649, 85655, 56731]
3  G-00003   gsd3            2         [34566, 78931]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pandas: how to get the unique values of a column that contains a list of values?

From Dev

Pandas - Count and get unique occurrences of string values from a column

From Dev

Count of unique values per group as new column with pandas

From Dev

how to replace column values with dictionary keys in pandas

From Dev

Pandas GroupBy and add count of unique values as a new column

From Dev

How extract values of dictionary column in pandas dataframe

From Dev

Pandas: How to group by one column and show count for unique values for all other columns per group?

From Dev

How to iterate over unique values in pandas and count frequency of associated values

From Dev

Is there a way to use the unique values of a count of occurences into column headers pandas?

From Dev

How to count the unique values of a column and append each value to a dictionary?

From Dev

Count unique values by column

From Dev

How to add to a count based on column values pandas

From Dev

How to extract unique values from pandas column where values are in list

From Dev

How to get frequency count of column values for each unique pair of columns in pandas?

From Dev

Getting count of unique values in pandas Dataframe when there is a list object in a column

From Dev

Count unique values for every column

From Dev

Creating Dictionary from Pandas DataFrame Column Based on Unique Values in Column

From Dev

How to count not null values on pandas column then agregating

From Dev

Cumulative count of unique values in pandas

From Dev

DataFrame: How to add a column whose values are a unique count of another column?

From Dev

How to create pandas column values in dictionary?

From Dev

How to count number of unique values in column based on values in other column

From Dev

How to replace pandas dataframe column values based on dictionary key and values?

From Dev

pandas count unique values considering column

From Dev

How to groupby a column and count the number of unique values in another column

From Dev

How to count unique values in a dictionary of lists with Pandas?

From Dev

Count unique values in csv column without pandas

From Dev

How to count unique values in one colulmn based on value in another column by group in Pandas

From Dev

How to filter a pandas dataframe by unique column values

Related Related

  1. 1

    Pandas: how to get the unique values of a column that contains a list of values?

  2. 2

    Pandas - Count and get unique occurrences of string values from a column

  3. 3

    Count of unique values per group as new column with pandas

  4. 4

    how to replace column values with dictionary keys in pandas

  5. 5

    Pandas GroupBy and add count of unique values as a new column

  6. 6

    How extract values of dictionary column in pandas dataframe

  7. 7

    Pandas: How to group by one column and show count for unique values for all other columns per group?

  8. 8

    How to iterate over unique values in pandas and count frequency of associated values

  9. 9

    Is there a way to use the unique values of a count of occurences into column headers pandas?

  10. 10

    How to count the unique values of a column and append each value to a dictionary?

  11. 11

    Count unique values by column

  12. 12

    How to add to a count based on column values pandas

  13. 13

    How to extract unique values from pandas column where values are in list

  14. 14

    How to get frequency count of column values for each unique pair of columns in pandas?

  15. 15

    Getting count of unique values in pandas Dataframe when there is a list object in a column

  16. 16

    Count unique values for every column

  17. 17

    Creating Dictionary from Pandas DataFrame Column Based on Unique Values in Column

  18. 18

    How to count not null values on pandas column then agregating

  19. 19

    Cumulative count of unique values in pandas

  20. 20

    DataFrame: How to add a column whose values are a unique count of another column?

  21. 21

    How to create pandas column values in dictionary?

  22. 22

    How to count number of unique values in column based on values in other column

  23. 23

    How to replace pandas dataframe column values based on dictionary key and values?

  24. 24

    pandas count unique values considering column

  25. 25

    How to groupby a column and count the number of unique values in another column

  26. 26

    How to count unique values in a dictionary of lists with Pandas?

  27. 27

    Count unique values in csv column without pandas

  28. 28

    How to count unique values in one colulmn based on value in another column by group in Pandas

  29. 29

    How to filter a pandas dataframe by unique column values

HotTag

Archive