python pandas:将字典展平为单个列

时间:2016-05-27 12:42:49

标签: python-2.7 dictionary pandas

我正在编写代码以从组织的数据(包括部门和部门)获取excel文件中的数据 - 有一个关键:div和dept之间的值关系 - 并将其放在模板中,为每个组织分配ID号。分部和部门在同一列中,它们的关系在“parentID”字段中注明,在下面的示例中。

id                                               section            parentID    
10000000000 <-company: root level                Company            10000000000 
10010000000 <-division - level 1                 Operations         10000000000 
10010001000 <-department - level 2               Manufacturing      10010000000  
10010001000 <-department - level 2               Distribution       10010000000  
10020000000 <-division - level 1                 Finance            10000000000 
10020001000 <-department - level 2               Payroll            10020000000 
10020002000 <-department - level 2               Accounts Payable   10020000000 

我有代码通过创建它们的唯一列表然后应用ID来为分区生成ID。

df['Division'] .replace('', np.nan, inplace=True)
df.dropna(subset=['Division'], inplace=True)
df1 = df[['Division','Department']].drop_duplicates()
df1 = df1.sort_values(by=['Division','Department'],ascending=[True, True])

#get unique list of divisions
df2 = pd.DataFrame(df1['Division'].unique())

#set id list for divisions
dfinst = range(len(df2))

for row in df2:    
    iids = dfinst
    sectionID = []
for iid in iids:
    iid = iid +1
    iid = "%02d"%iid 
    inst_ID = '10' + str(iid) + '00000000'
    sectionID.append(inst_ID)
df2['sectionID'] =pd.DataFrame(sectionID)
df2.rename(columns = {0:'cfName'}, inplace = True)
df2['parentID'] = '100000000000'#sets company's id as parent ID for all divisions

以下代码创建了一个分部和部门的字典,我将其转换为数据框:

df1 = df1.reset_index(level=1)
df3 = pd.Series(df1['Department'].groupby(df1['Division']))
df3 = df3.to_frame()

当我将其导出到Excel时,字典看起来如下(包括间距):

"(u'Operations', 0    Manufacturing1                       Distribution

Name: Division, dtype: object)"
"(u'Finance', 2    Payroll3                       Accounts Payable

Name: Division, dtype: object)"

如何展平该字典,以便我可以在“部分”栏中找到所有部门和部门?

编辑:

(u'Operations', 0    Manufacturing
1                       Distribution
Name: Division, dtype: object)
(u'Finance', 0    Payroll
1                       Accounts Payable
Name: Primary Assignment Center, dtype: object)

0 个答案:

没有答案