Home > OS >  Counting occurrences of value in dataframe column from last row of dataframe
Counting occurrences of value in dataframe column from last row of dataframe

Time:10-19

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.

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.

CodePudding user response:

You can compare last row selected by DataFrame.iloc in DataFrame.eq and counts Trues values by sum:

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

CodePudding user response:

Use 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.

CodePudding user response:

Try using iloc with sum:

df.eq(df.iloc[-1]).sum().tolist()
  • Related