简体   繁体   English

如果列元素是一个集合,如何从 pandas 数据框列中获取每个值的计数?

[英]How do I get count of each value from a pandas Data Frame column if the column elements is a set?

I have a Data set which has a column whose elements are a set like below我有一个数据集,其中有一列的元素如下所示

index  set_column
0    {5520986, 5520236}
1    {5520986, 5520236}
2    {5471829, 5515351}
3    {5471829, 5515351}
4    {5471829, 5515351}
5    {5471829, 5515351}
6    {5471829, 5515351}
7    {5471829, 5515351}

what i am trying to get is count of each unique values.我想要得到的是每个唯一值的计数。

 set            count
{5520986, 5520236} 2
{5471829, 5515351} 6

You can do:你可以做:

df = df["set_column"].value_counts().reset_index()
df.columns = ["set", "count"]

print(df)

Output: Output:

                  set  count
0  {5471829, 5515351}      6
1  {5520986, 5520236}      2

You can simply try this你可以简单地试试这个

df['Count']=df.groupby().cumcount()
df=df.groupby(['set_column'].max()

暂无
暂无

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

相关问题 更改 pandas 数据框以从 pandas 中的数据框添加最大列 -> 一年中每个月的最大值。 我怎样才能做到这一点? - Change pandas data frame to add max column -> maximum value for each month of the year from a data frame in pandas. How can I do this? 如何按列的值对pandas数据帧的行进行分组? - How do I group the rows of a pandas data frame by a value of a column? 从熊猫数据框中获取单词列表的计数,其中每一列都是单词列表 - TO get count of list of words from a pandas data frame where each column is a list of words 根据从末尾开始的列值的计数过滤 Pandas 数据框 - Filter a pandas data frame based on the count of a column value from the end 如何用列值替换pandas数据框中的每个值? - How to replace each value in pandas data frame with column value? 如何计算特定名称出现在 Pandas 数据框列中的次数? - How do I count the number of times a specific name appears in a pandas data frame column? 如何在 Pandas 中获取组中最大数据的列的值? - How do I get the value of the column with the max data in a group by in Pandas? python - 如何在python pandas中分组并取一列的计数除以数据框第二列的唯一计数? - How to do group by and take Count of one column divide by count of unique of second column of data frame in python pandas? 如何在 pandas 数据帧的列中的每个数字后添加一个“单位符号”? - How do I add a " unit symbol after each number in a column in a pandas data frame? 从pandas数据框列的词典列表中获取第一个值 - Get first value from a list of dictionaries in pandas data frame column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM