Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
418 B
1
Indexable
Never
import pandas as pd

# create sample data
data = {
    'id': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
    'industry': ['X', 'X', 'Y', 'Y', 'Y', 'Z', 'Z', 'Z'],
    'rank': [1, 2, 1, 2, 3, 1, 2, 3]
}

df = pd.DataFrame(data)

# calculate rank percentage by industry
df['rank_percentage'] = df.groupby('industry')['id'].transform(lambda x: df.loc[x.index, 'rank'] / len(x))

# output the result
print(df)