[英]Pandas python converting a list into a dataframe and count number of occurrences
This such a simple problem, which is why it is annoying me to no end that I can't do it.这是一个如此简单的问题,这就是为什么我无法做到这一点让我无休止的烦恼。
I have a series of lists that I have converted into a flattened list:我有一系列列表已转换为扁平列表:
myList= []
for i in myList['variable'].tolist():
for j in i:
myList.append(j)
frequency = pd.DataFrame(myList).reset_index().sort_values(0).groupby(0).count()
It's the last line that is letting me down.这是让我失望的最后一行。 I don't know why, but it returns a wrongful count of each member of the group.
我不知道为什么,但它返回了组中每个成员的错误计数。
sample input = [2, 4, 6, 7, 2, 6, 2]
样本输入 =
[2, 4, 6, 7, 2, 6, 2]
expected DataFrame output =预计 DataFrame output =
var count
2 3
4 1
6 2
7 1
If you'd like to group by a categorical variable and compute aggregate statistics for each group, pandas can do that:如果您想按分类变量分组并计算每个组的聚合统计信息,pandas 可以这样做:
df = pd.DataFrame({
'a': random.choices(range(1,4), k=20),
'b': np.random.normal(size=20),
})
df.groupby('a').count()
# b
# a
# 1 2
# 2 8
# 3 10
df.groupby('a').agg(('count', 'mean', 'std'))
# b
# count mean std
# a
# 1 2 0.454240 2.218616
# 2 8 0.165295 1.355922
# 3 10 -0.004880 1.225908
As this website was so helpful, I figured it out.由于这个网站很有帮助,我想通了。
Test = pd.DataFrame(list) Frequency = test[0].value_counts()测试 = pd.DataFrame(list) 频率 = test[0].value_counts()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.