简体   繁体   English

如何计算数据框中另一列中每个唯一值对应的值?

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

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...干杯...

Use for number of unique values per groups use GroupBy.transform with DataFrameGroupBy.nunique :用于每个组的唯一值数量使用GroupBy.transformDataFrameGroupBy.nunique

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

Use for count values per groups use DataFrameGroupBy.size :用于每个组的计数值使用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

Here is another option similar to Jezrael, who beat me to it!这是另一个类似于 Jezrael 的选项,他打败了我!

import pandas as pd 
a = {'Car type':['VW','VW','VW','AUDI','AUDI','AUDI'],'Color':['Blue','Red','Black','Silver','Black','White'],'ID':[123,567,779,112,356,224]}
df = pd.DataFrame(a)
print(df)
df_a = df.merge(df.groupby(['Car type'],as_index=False).agg({'Color':'nunique'}),how='left',on='Car type').rename(columns={'Color_x':'Color','Color_y':'Unique_colors'})

Output:输出:

  Car type   Color   ID  Unique_colors
0       VW    Blue  123              3
1       VW     Red  567              3
2       VW   Black  779              3
3     AUDI  Silver  112              3
4     AUDI   Black  356              3
5     AUDI   White  224              3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将列的唯一值和 append 每个值计算到字典中? - How to count the unique values of a column and append each value to a dictionary? 对于列中的每个值,如何计算其行中唯一值的数量? - For each value in a column, how to count the number of unique values in its row? Python Pandas - 过滤 pandas dataframe 以获取一列中具有最小值的行,以获取另一列中的每个唯一值 - Python Pandas - filter pandas dataframe to get rows with minimum values in one column for each unique value in another column 如何制作 0 和 1 的 dataframe 使得每个唯一值都是一列? - How to make a dataframe of 0 and 1 such that each unique value is a column? 如何在python中使用另一个数据帧的列的唯一列值和值计数制作数据帧? - How to make a dataframe in python with unique column values and value counts of a column of another dataframe? 如何用同一列中的值填充 null 列中的 Pyspark Dataframe 值,其在另一列中的对应值相同 - How to fill null values in a Pyspark Dataframe column with values from the same column, whose corresponding value in another column is same Python pandas dataframe:为另一列的每个唯一值查找最大值 - Python pandas dataframe: find max for each unique values of an another column 计算DataFrame列中所有定义的值,其中pandas中未定义另一列中的相应值 - Count all defined values in a DataFrame column where the corresponding values in another column are undefined in pandas 如何对 dataframe 列中的多个值求和,如果它们对应于另一列中的 1 个值 - How to sum multiple values in a dataframe column, if they are corresponding to 1 value in an other column 对于每个类别,如何找到另一列的最小值对应的列的值? - For each category, how to find the value of a column corresponding to the minimum of another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM