[英]Counting occurrences of value in dataframe column from last row of dataframe
I have a dataframe with columns of integers.我有一个包含整数列的数据框。 I want to count how many times the value in the last row appears in the column and return a list.我想计算最后一行中的值出现在列中的次数并返回一个列表。 Example:例子:
df = [[1, 2, 3, 4], [1, 3, 4, 2], [2, 2, 4, 1], [1, 2, 3, 4], [3, 4, 4, 2], [1, 3, 4, 1]]
df = pd.DataFrame(df)
print(df)
0 1 2 3
0 1 2 3 4
1 1 3 4 2
2 2 2 4 1
3 1 2 3 4
4 3 4 4 2
5 1 3 4 1
Desired result would be a list.期望的结果将是一个列表。 As an example, the last value in column 0 is a 1, there are 4 1's in that column so the first element in the list would be 4.例如,第 0 列中的最后一个值是 1,该列中有 4 个 1,因此列表中的第一个元素将为 4。
result = [4, 2, 4, 2]
I have a function that compares all values in the dataframe and gets their occurrence count but I really only care about the last row and how many times they appear.我有一个函数可以比较数据框中的所有值并获取它们的出现次数,但我真的只关心最后一行以及它们出现的次数。 Any help would be greatly appreciated and I hope this made sense.任何帮助将不胜感激,我希望这是有道理的。
You can compare last row selected by DataFrame.iloc
in DataFrame.eq
and counts True
s values by sum
:您可以比较DataFrame.iloc
在DataFrame.eq
选择的最后一行,并通过sum
计算True
s 值:
print(df.eq(df.iloc[-1]).sum().tolist())
[4, 2, 4, 2]
Details :详情:
print (df.iloc[-1])
0 1
1 3
2 4
3 1
Name: 5, dtype: int64
print(df.eq(df.iloc[-1]))
0 1 2 3
0 True False False False
1 True True True False
2 False False True True
3 True False False False
4 False False True False
5 True True True True
print(df.eq(df.iloc[-1]).sum())
0 4
1 2
2 4
3 2
dtype: int64
Use apply
:使用apply
:
>>> df.apply(lambda x: x.tolist().count(x.iloc[-1]))
0 4
1 2
2 4
3 2
dtype: int64
>>>
Original answer:原答案:
df.eq(df.iloc[-1]).sum().tolist()
@jezrael posted 6 seconds before mine, check his out for a better solution. @jezrael 比我早 6 秒发布,查看他的更好的解决方案。
尝试将iloc
与sum
iloc
使用:
df.eq(df.iloc[-1]).sum().tolist()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.