如何从Python数据框中提取和分离随机元组值?

数据主题中有两个值(例如,主题和主题类别)作为列存在。与此相关的是,我在另一列中介绍了主题的重要性。 我希望创建另一个数据框,该数据框包含一个主题的随机实例,并根据该主题的权重分配相应的主题类别。对我而言,最棘手的部分是将两个值(主题和主题类别)与权重一起使用。虽然我能够提取元组的值并根据权重生成元组的随机实例。我无法将元组分成其组成元素以插入到最终数据框中。 “邮编”功能不起作用。

在我的输出数据框中,我希望将序列号,主题和主题类别作为单独的列。 如果您对如何更好地解决此类问题有一些想法,我将不胜感激。

import random
import pandas as pd

data=[['Agricultural services', 'Agricultural services, inputs, tools and equipment',   1],
['Agriculture primary production(livestock)',   'Agricultural services, inputs, tools and equipment',7],
['Assist Uganda in upgrading its coffee and cocoa value chains',    'Agricultural services, inputs, tools and equipment',1],
['Building materials and agricultural tools Building and civil works', 'carpentry, construction materials, maintenance, renovation, road works',1],
['Clearing and forwarding services','Clearing and forwarding services', 1],
['Collection of revenue from big slaughters Collection of fees', 'taxes and revenue', 1],
['Collection of revenue from chicken sellers','Collection of fees, taxes and revenue',  19]]
tender_subject = pd.DataFrame(data, columns = ['sub', 'sub_category','subject_dist']) 

subject_tuple=list(tender_subject[['sub', 'sub_category']].itertuples(index=False, name=None)) #we could have also used tuple here instead of 'list'
subject_weights=tender_subject['subject_dist'].tolist()
data={'SL':[], 'Subject':[],'subject_category':[],}
output_df=pd.DataFrame(data)
x=0
for i in range(10):
    p=random.choices(subject_tuple,subject_weights)  
    p1,p2=zip(p) # This line is not working

    output_df.loc[x]=[i]+[p1]+[p2]
    x=x+1
print(output_df.head)
评论