data = [['BAL', 'BAL', 'NO', 'DAL'], ['DAL', 'DAL', 'TEN', 'SF']] df = pd.DataFrame(data)
I want to count the number of occurrences of the value in the first column in each row, across that row.
In this example, the number of times “BAL” appears in the first row, “DAL” in the second row, etc.
Then assign that count to a new column
You could do something like this:
Create a series using
iloc of the first column of your dataframe, then compare values use pd.DataFrame.eq with axis=0 and
sum along axis=1.
0 1 2 3 count 0 BAL BAL NO DAL 2 1 DAL DAL TEN SF 2