Count number of occurrences of first column value respectively across each row

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 df['Count'].

Answer

You could do something like this:

df.assign(count=df.eq(df.iloc[:,0], axis=0).sum(axis=1))

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.

Output:

     0    1    2    3  count
0  BAL  BAL   NO  DAL      2
1  DAL  DAL  TEN   SF      2