Pivot pandas Dataframes with non-numeric values

Setup

# Import libraries
import pandas as pd
import numpy as np

# Sample DataFrame
df = pd.DataFrame({
    'advertiser':['Alice', 'Alice', 'Alice', 'Alice', 'Bob', 'Bob', 'Bob', 'Charlie', 'Charlie', 'Charlie'],
    'keyword': ['screen', 'phone', 'laptop', 'keyboard', 'phone', 'tablet', 'flat', 'screen', 'phone', 'computer'],
    'clicks': np.random.randint(low=20, high=50, size=10)
})
df
advertiser keyword clicks
0 Alice screen 34
1 Alice phone 46
2 Alice laptop 34
3 Alice keyboard 36
4 Bob phone 24
5 Bob tablet 33
6 Bob flat 25
7 Charlie screen 35
8 Charlie phone 25
9 Charlie computer 35

Pivot keywords to columns

# Pivot keywords to columns
(
    df
    .pivot(index='advertiser', columns='keyword', values='clicks')
)
keyword computer flat keyboard laptop phone screen tablet
advertiser
Alice NaN NaN 36.0 34.0 46.0 34.0 NaN
Bob NaN 25.0 NaN NaN 24.0 NaN 33.0
Charlie 35.0 NaN NaN NaN 25.0 35.0 NaN
# Pivot keywords to columns, ranked by clicks
(
    df
    .sort_values('clicks', ascending=False)
    .groupby('advertiser')
    .agg(list)
    .apply(lambda x: pd.Series(x['keyword']), axis=1)
)
0 1 2 3
advertiser
Alice phone keyboard screen laptop
Bob tablet flat phone NaN
Charlie screen computer phone NaN