简体   繁体   中英

Add rank to dataframe of IDs

I have a dataframe of just IDs eg

data=pd.DataFrame({'ID':['D29305C3-6652-E911-B81F-005056962850','570AE90B-CB53-EA11-B836-005056962850','5F21D4D2-E156-EA11-B836-005056962850','73579A31-1252-E911-B81F-005056962850']})

I want to add a row from 1-30 for each ID. I tried making a separate list and joining it (range is manually worked out as 30 x number of IDs):

numbers=pd.DataFrame({'Integers':[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]})

numbers2=pd.DataFrame()
for x in range (1,120):
    numbers2=numbers2.append(numbers)
numbers2=numbers2.reset_index()

df=pd.DataFrame()

from collections import Counter
id_count = Counter(data['ID'])
# Create lists of each id repeated the number of times each is needed:
n = 30
id_values = [[i] * (n - id_count[i]) for i in id_count.keys()]
# Flatten to a single list:
id_values = [i for s in id_values for i in s]
# Create as new DataFrame and append to existing data:
new_data = pd.DataFrame({"ID": id_values})
df = df.append(new_data).sort_values(by="ID")

df=df.reset_index()

template=pd.merge(df, numbers2, left_index=True, right_index=True)

Where I worked out the range manually Which works sometimes but eg for this ID has behaviour I don't understand:

template[template.ID=='D29305C3-6652-E911-B81F-005056962850']

And it's a clunky way to attempt it in any case. Thanks for any suggestions: :)

Let us do cross merge

data.merge(pd.Series(range(1, 31), name='rank'), how='cross')

                                       ID  rank
0    D29305C3-6652-E911-B81F-005056962850     1
1    D29305C3-6652-E911-B81F-005056962850     2
2    D29305C3-6652-E911-B81F-005056962850     3
3    D29305C3-6652-E911-B81F-005056962850     4
4    D29305C3-6652-E911-B81F-005056962850     5
5    D29305C3-6652-E911-B81F-005056962850     6
6    D29305C3-6652-E911-B81F-005056962850     7
...
118  73579A31-1252-E911-B81F-005056962850    29
119  73579A31-1252-E911-B81F-005056962850    30

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM