如何在python中的多处理中执行以下功能

时间:2019-07-26 11:15:20

标签: python pandas python-multiprocessing

我想在python中使用多处理池执行以下功能。

df = pd.DataFrame({'M': ['A','A','A', 'B', 'C', 'C', 'D', 'D'], 'P' : ['A1', 'B1', 'C1', 'B1','A1', 'B1, 'C1', 'D1'], 'R' : [2,2,4,3,4,6,8,4], 'T' : [3,4,6,5,5,8,10,4]})

l1 = df['M'].unique() 
p_list = ['P' + str(i) for i in range(0, 4)]
y = pd.DataFrame(columns = p_list)
y = y.rename(columns ={'P0' : 'M'})
for l in l1:
    temp = df[df['M'] == l]
    x = temp.pivot_table(index='M', columns=['P'], values=['R', 'T'], fill_value = 0).sort_index(level=0, axis=0)
    flattened = pd.DataFrame(x.sort_index(level=1, axis=1).to_records())
    flattened = flattened.append(dict(zip(flattened.columns, [col[:] for col in flattened])), ignore_index=True)
    flattened = flattened.reindex([1,0]).reset_index(drop = True)
    flattened['M'] = flattened['M'].str.replace('M', '')
    flattened = flattened.rename(columns = {column_name: 'P' + str(index) for index,column_name in enumerate(flattened.columns)})
    flattened = flattened.rename(columns = {flattened.columns[0]: 'M'})
    y = pd.concat([y,flattened], axis = 0)

0 个答案:

没有答案