Pandas Count The Occurrences Of Each Value In Column
I have this dataframe: I want to have a new column that counts only the first instances of the matchID in the column MatchID. Specifically, it checks the matchID to see if it is
Solution 1:
How about:
df['Count'] = (~df['MatchID'].duplicated()).astype(int)
Solution 2:
Here's an approach based on a sample DataFrame:
# Some dummy data. The field ID is equivalent to MatchIDdf = pd.DataFrame([("A",12),("B", 12), ("A",123)], columns=["id","val"])
# Create a temporary subset of the DF that matches the "first or unique" rule
first_or_unique = df.drop_duplicates(subset="id", keep="first")
# Populate the new match lookup series with 0 for all rows to begin withdf["match"] = 0
# Finally, use `.loc` along with the temporary DF's index to set the relevant# rows to be 1
df.loc[first_or_unique.index.values, "match"] = 1
Post a Comment for "Pandas Count The Occurrences Of Each Value In Column"