pandas 将等级添加到ID的 Dataframe

kpbwa7wx  于 2022-11-27  发布在  其他
关注(0)|答案(1)|浏览(140)

我有一个只有ID的 Dataframe ,例如

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

我想为每个ID添加1-30行。我试着创建一个单独的列表并将其加入(范围手动计算为30 x ID数):

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)

我手动计算了范围,有时可以工作,但例如,对于此ID,我不理解其行为:

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

无论如何,这都是一种笨拙的尝试方式。谢谢你的任何建议!:)

相关问题