I use groupby
and size
df.groupby(['id', 'group', 'term']).size().unstack(fill_value=0)
Timing
1,000,000 rows
df = pd.DataFrame(dict(id=np.random.choice(100, 1000000),
group=np.random.choice(20, 1000000),
term=np.random.choice(10, 1000000)))
More Related Contents:
- Get statistics for each group (such as count, mean, etc) using pandas GroupBy?
- Aggregation in Pandas
- Converting a Pandas GroupBy output from Series to DataFrame
- Multiple aggregations of the same column using pandas GroupBy.agg()
- Pandas groupby mean – into a dataframe?
- Python Pandas max value in a group as a new column
- How to loop over grouped Pandas dataframe?
- How to access pandas groupby dataframe by key
- Pandas Groupby and Sum Only One Column
- How to drop duplicates based on two or more subsets criteria in Pandas data-frame
- Python Pandas Group by date using datetime data
- Pandas get frequency of item occurrences in a column as percentage [duplicate]
- Pandas – dataframe groupby – how to get sum of multiple columns
- Group dataframe and get sum AND count?
- How to summarize on different groupby combinations?
- get first and last values in a groupby
- Pandas groupby and aggregation output should include all the original columns (including the ones not aggregated on)
- Get the row corresponding to the max in pandas GroupBy [duplicate]
- How to get number of groups in a groupby object in pandas?
- When is it appropriate to use df.value_counts() vs df.groupby(‘…’).count()?
- How to iterate over rows in a DataFrame in Pandas
- extract column value based on another column pandas dataframe
- Coalesce values from 2 columns into a single column in a pandas dataframe
- python pandas – creating a column which keeps a running count of consecutive values
- Turn Pandas Multi-Index into column
- What is the fastest and most efficient way to append rows to a DataFrame?
- Check if rows in one dataframe exist in another dataframe
- Pandas – Interleave / Zip two DataFrames by row
- Pandas custom function to find whether it is the 1st, 2nd etc Monday, Tuesday, etc – all suggestions welcome
- How to return a csv file/Pandas DataFrame in JSON format using FastAPI?