How to count the values corresponding to each unique value in another column in a dataframe?

edgar lenin lizarraga gastelum

i have a table like this:

Car Type |  Color  |  ID
 VW      |  Blue   | 123
 VW      |  Red    | 567
 VW      |  Black  | 779
 -----------------------
 AUDI    | Silver  | 112
 AUDI    | Black   | 356
 AUDI    | White   | 224

how can i get something like this? where each row contains the count of colors for each car type?

Car Type |  Color  |  ID | Total
 VW      |  Blue   | 123 |  3
 VW      |  Red    | 567 |  3
 VW      |  Black  | 779 |  3
 -----------------------
 AUDI    | Silver  | 112 |  3
 AUDI    | Black   | 356 |  3
 AUDI    | White   | 224 |  3

Cheers...

jezrael

Use for number of unique values per groups use GroupBy.transform with DataFrameGroupBy.nunique:

df['Total'] = df.groupby('Car Type')['Color'].transform('nunique')

Use for count values per groups use DataFrameGroupBy.size:

df['Total'] = df.groupby('Car Type')['Color'].transform('size')

Difference with changed one value:

df['Total_uniq'] = df.groupby('Car Type')['Color'].transform('nunique')
df['Total_size'] = df.groupby('Car Type')['Color'].transform('size')
print (df)
  Car Type   Color   ID  Total_uniq  Total_size
0       VW    Blue  123           2           3
1       VW    Blue  567           2           3 <- set value to Blue
2       VW   Black  779           2           3
3     AUDI  Silver  112           3           3
4     AUDI   Black  356           3           3
5     AUDI   White  224           3           3

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Can I divide each column of dataframe using corresponding values from another dataframe in R?

分類Dev

Finding counts of unique values in column for each unique value in other column

分類Dev

How to count nonblank values in each dataframe row

分類Dev

Add a new column to dataframe and add unique values to each row

分類Dev

Add a new column to dataframe and add unique values to each row

分類Dev

Pandas dataframe count values for each column and sum in new index

分類Dev

How to join unique strings from a column in dataframe based on another column

分類Dev

How to check values of column in one dataframe available or not in column of another dataframe?

分類Dev

Set values of column in dataframe according to the order of another column value in python

分類Dev

How to assign a values to dataframe's column by comparing values in another dataframe

分類Dev

How do I count inside a dataframe using unique values and conditional?

分類Dev

Find rows of a dataframe that have same non-unique column values as a column in another dataframe

分類Dev

How to print incremental count of occurrences of unique values in column 1

分類Dev

How to put unique values of 1 series as columns and count each occurence of the unique values from the series per quarter?

分類Dev

Filling in NAs with corresponding row values in another column

分類Dev

How to repeat all column values for each row in another column

分類Dev

How to find corresponding column value if value exists in another column of same table

分類Dev

Find all combinations of one column based on the unique values of another column in a dataframe

分類Dev

Repeat dataframe rows n times according to the unique column values and to each row repeat create a new column with different values

分類Dev

Select MIN, MAX Corresponding column based on another column values

分類Dev

Is there a way in pandas to groupby and then count unique where another column has a specified value?

分類Dev

R Loop over unique values in a dataframe column to create another one based on conditions

分類Dev

Finding a specific value for each row in a Dataframe from another Dataframe's column

分類Dev

SQL: count rows where column = a value AND another column is the same as values in the group where the first condition is true?

分類Dev

How to decile python pandas dataframe by column value, and then sum each decile?

分類Dev

Count combination of variables based on unique column value

分類Dev

Use Spark to group by consecutive same values of one column, taking Max or Min value of another column for each group

分類Dev

Finding the index or unique values from a dataframe column

分類Dev

Save in DataFrame unique values for every column

Related 関連記事

  1. 1

    Can I divide each column of dataframe using corresponding values from another dataframe in R?

  2. 2

    Finding counts of unique values in column for each unique value in other column

  3. 3

    How to count nonblank values in each dataframe row

  4. 4

    Add a new column to dataframe and add unique values to each row

  5. 5

    Add a new column to dataframe and add unique values to each row

  6. 6

    Pandas dataframe count values for each column and sum in new index

  7. 7

    How to join unique strings from a column in dataframe based on another column

  8. 8

    How to check values of column in one dataframe available or not in column of another dataframe?

  9. 9

    Set values of column in dataframe according to the order of another column value in python

  10. 10

    How to assign a values to dataframe's column by comparing values in another dataframe

  11. 11

    How do I count inside a dataframe using unique values and conditional?

  12. 12

    Find rows of a dataframe that have same non-unique column values as a column in another dataframe

  13. 13

    How to print incremental count of occurrences of unique values in column 1

  14. 14

    How to put unique values of 1 series as columns and count each occurence of the unique values from the series per quarter?

  15. 15

    Filling in NAs with corresponding row values in another column

  16. 16

    How to repeat all column values for each row in another column

  17. 17

    How to find corresponding column value if value exists in another column of same table

  18. 18

    Find all combinations of one column based on the unique values of another column in a dataframe

  19. 19

    Repeat dataframe rows n times according to the unique column values and to each row repeat create a new column with different values

  20. 20

    Select MIN, MAX Corresponding column based on another column values

  21. 21

    Is there a way in pandas to groupby and then count unique where another column has a specified value?

  22. 22

    R Loop over unique values in a dataframe column to create another one based on conditions

  23. 23

    Finding a specific value for each row in a Dataframe from another Dataframe's column

  24. 24

    SQL: count rows where column = a value AND another column is the same as values in the group where the first condition is true?

  25. 25

    How to decile python pandas dataframe by column value, and then sum each decile?

  26. 26

    Count combination of variables based on unique column value

  27. 27

    Use Spark to group by consecutive same values of one column, taking Max or Min value of another column for each group

  28. 28

    Finding the index or unique values from a dataframe column

  29. 29

    Save in DataFrame unique values for every column

ホットタグ

アーカイブ