如果您对Pandas列中唯一值的计数感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于Pandas列中唯一值的计数的详细内容,我们还将为您解答pandas唯一值的相关问题,并且
如果您对Pandas列中唯一值的计数感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于Pandas列中唯一值的计数的详细内容,我们还将为您解答pandas 唯一值的相关问题,并且为您提供关于asp.net – LINQ:列表中唯一项目的计数、delphi – 检测数组中唯一值的数量、numpy:数组中唯一值的最有效频率计数、n维数组中唯一值的索引的有价值信息。
本文目录一览:- Pandas列中唯一值的计数(pandas 唯一值)
- asp.net – LINQ:列表中唯一项目的计数
- delphi – 检测数组中唯一值的数量
- numpy:数组中唯一值的最有效频率计数
- n维数组中唯一值的索引
Pandas列中唯一值的计数(pandas 唯一值)
我有一个数据框,正在查看数据框中的一列,即名称
array([''Katherine'', ''Robert'', ''Anne'', nan, ''Susan'', ''other''], dtype=object)
我试图打个电话告诉我这些唯一名称在该列中出现了多少次,例如,如果有223个Katherine实例等,我该怎么做?我知道value_counts只是为每个显示1,因为它们是单独的唯一值
答案1
小编典典如果我对您的理解正确,则可以使用pandas.Series.value_counts。
例:
import pandas as pdimport numpy as nps = pd.Series([''Katherine'', ''Robert'', ''Anne'', np.nan, ''Susan'', ''other''])s.value_counts()Katherine 1Robert 1other 1Anne 1Susan 1dtype: int64
您提供的数据只有每个名称之一-因此,这里是带有多个“凯瑟琳”条目的示例:
s = pd.Series([''Katherine'',''Katherine'',''Katherine'',''Katherine'', ''Robert'', ''Anne'', np.nan, ''Susan'', ''other''])s.value_counts()Katherine 4Robert 1other 1Anne 1Susan 1dtype: int64
应用于数据 框后, 您将按以下方式调用此方法:
df[''names''].value_counts()
asp.net – LINQ:列表中唯一项目的计数
例:
List<string> aryIDs = new List<string>; aryIDs.Add("1234"); aryIDs.Add("4321"); aryIDs.Add("3214"); aryIDs.Add("1234"); aryIDs.Add("4321"); aryIDs.Add("1234");
会产生:
"1234",3 "4321",2 "3214",1
这在Tsql中很容易,但如果可能,我想避免服务器往返,不必要的表等.
提前致谢.
更新:对于Ralph Shillington的回答如下:
Dim result = From id In aryIDs _ Group id By id Into Group _ Order By Group.Count() Descending _ Select id,Count = Group.Count() result.Dump()
解决方法
List<string> aryIDs = new List<string>(); aryIDs.Add("1234"); aryIDs.Add("4321"); aryIDs.Add("3214"); aryIDs.Add("1234"); aryIDs.Add("4321"); aryIDs.Add("1234"); var result = from id in aryIDs group id by id into g orderby g.Count() descending select new { Id=g.Key,Count=g.Count() }; result.Dump();
剪切并粘贴到LinqPad,你应该很好去.
也可以使用Lambda表达式来写:
aryIDs.GroupBy (id => id).OrderByDescending (id => id.Count()).Select(g => new { Id=g.Key,Count=g.Count()}).Dump();
delphi – 检测数组中唯一值的数量
我目前的做法:
> Quicksort整数数组
>然后运行循环来比较元素.
在代码中:
yearHolder := ''; for I := 0 to High(yeararray) do begin currYear := yeararray[i]; if (yearHolder <> currYear) then begin yearHolder := currYear; Inc(uniqueYearNumber); end; end;
解决方法
hl := THashedStringList.Create; // in Inifiles try hl.sorted := True; hl.Duplicates := dupIgnore; // ignores attempts to add duplicates for i := 0 to High(yeararray) do hl.Add(yeararray[i]); uniqueYearCount := hl.Count; finally hl.Free; end;
numpy:数组中唯一值的最有效频率计数
在 numpy
/中 scipy
,是否有一种 有效的 方法来获取数组中唯一值的频率计数?
遵循以下原则:
x = array( [1,1,2,5,25,1] )
y = freq_count( x )
print y
>> [[1,5],[2,3],[5,1],[25,1]]
(对于您来说,R用户在那里,我基本上是在寻找该table()
功能)
n维数组中唯一值的索引
我有一个2D Numpy数组,包含从0到n的值。我想要一个长度为n的列表,以使该列表的第i个元素是值i + 1(不包括0)的所有索引的数组。
例如,对于输入
array([[1,1],[2,2,0]])
我期望得到
[array([[0,0],[0,2]]),array([[1,[1,1]])]
我发现了一个相关的问题:
在numpy数组中获取重复元素的所有索引的列表,
这可能会有所帮助,但我希望找到一个更直接的解决方案,该方法不需要展平和排序数组,并且尽可能高效。
今天的关于Pandas列中唯一值的计数和pandas 唯一值的分享已经结束,谢谢您的关注,如果想了解更多关于asp.net – LINQ:列表中唯一项目的计数、delphi – 检测数组中唯一值的数量、numpy:数组中唯一值的最有效频率计数、n维数组中唯一值的索引的相关知识,请在本站进行查询。
本文标签: