find unique elements in pandas and their connection with other column 1

find unique elements in pandas and their connection with other column

df = pd.DataFrame({'author':['a', 'a', 'b'], 'subreddit':['sr1', 'sr2', 'sr2']})

>>> df
  author subreddit
0      a       sr1
1      a       sr2
2      b       sr2
...

Here is what the above code is Doing:
1. Create a dataframe with two columns, author and subreddit.
2. Create a new column, author_subreddit, which is the concatenation of author and subreddit.
3. Group by author_subreddit and count the number of rows in each group.
4. Sort the dataframe by the count column in descending order.
5. Reset the index so that the index is a range from 0 to the number of rows.
6. Drop the count column.
7. Rename the author_subreddit column to author.
8. Rename the index column to subreddit.
9. Sort the dataframe by subreddit in ascending order.
10. Print the dataframe.

Similar Posts