2017-09-15 76 views
2

我有一個數據幀,我想將數據融合到多個目標列中。下面的代碼我用Python熊貓將數據融化爲另一列中的多個列和庫侖名稱

grp2 = pd.lreshape(grp1, cols.groupby(cols.str.split('_').str[1])).sort_values('ACCT_NAME') 

上面一行我失去了列名

grp2 = pd.melt(grp1 , id_vars = ['Client' , 'Industry'] , var_name = "H Year" , value_name = 'Count') 

上面一行我不得到多個目標列

從DF

Client INDUSTRY 1H2016_6MO 2H2016_6MO 1H2017_6MO 2H2017_6MO 1H2016_12MO 2H2016_12MO 1H2017_12MO 2H2017_12MO 

XXX  AAA   1   0   0   0   1   1   0   0 

YYY  BBB   0   0   1   0   0   0   0   1 
ZZZ  CCC   1   1   0   0   0   0   1   1 

XXX  AAA   1   0   0   0   1   1   0   0 

TO DF

Client INDUSTRY Year_Half 6MO 12MO 
XXX  AAA   1H2016  2 2 
XXX  AAA   2H2016  0 2 
XXX  AAA   1H2017  0 0 
XXX  AAA   2H2017  0 0 
YYY  BBB   1H2016  0 0 
YYY  BBB   2H2016  0 0 
YYY  BBB   1H2017  1 0 
YYY  BBB   2H2017  0 1 
ZZZ CCC 1H2016 1 0 
ZZZ CCC 2H2016 1 0 
ZZZ CCC 1H2017 0 1 
ZZZ CCC 2H2017 0 1 

請告知解決這個。我見過的其他問題,但他們不採取列名成單獨列

回答

2

用途:


df = df.set_index(['Client','INDUSTRY']) 
df.columns = df.columns.str.split('_', expand=True) 
df = df.stack(0).reset_index().rename(columns={'level_2':'Year_Half'}) 
print (df) 
    Client INDUSTRY Year_Half 12MO 6MO 
0  XXX  AAA 1H2016  1 1 
1  XXX  AAA 1H2017  0 0 
2  XXX  AAA 2H2016  1 0 
3  XXX  AAA 2H2017  0 0 
4  YYY  BBB 1H2016  0 0 
5  YYY  BBB 1H2017  0 1 
6  YYY  BBB 2H2016  0 0 
7  YYY  BBB 2H2017  1 0 
8  ZZZ  CCC 1H2016  0 1 
9  ZZZ  CCC 1H2017  1 0 
10 ZZZ  CCC 2H2016  0 1 
11 ZZZ  CCC 2H2017  1 0 
12 XXX  AAA 1H2016  1 1 
13 XXX  AAA 1H2017  0 0 
14 XXX  AAA 2H2016  1 0 
15 XXX  AAA 2H2017  0 0 

如果只有6MO12MO值和列的順序是非常重要的:

df = df.set_index(['Client','INDUSTRY']) 
df.columns = df.columns.str.split('_', expand=True) 
df = (df.stack(0) 
     .reindex_axis(['6MO','12MO'], 1) 
     .reset_index() 
     .rename(columns={'level_2':'Year_Half'})) 
print (df) 
    Client INDUSTRY Year_Half 6MO 12MO 
0  XXX  AAA 1H2016 1  1 
1  XXX  AAA 1H2017 0  0 
2  XXX  AAA 2H2016 0  1 
3  XXX  AAA 2H2017 0  0 
4  YYY  BBB 1H2016 0  0 
5  YYY  BBB 1H2017 1  0 
6  YYY  BBB 2H2016 0  0 
7  YYY  BBB 2H2017 0  1 
8  ZZZ  CCC 1H2016 1  0 
9  ZZZ  CCC 1H2017 0  1 
10 ZZZ  CCC 2H2016 1  0 
11 ZZZ  CCC 2H2017 0  1 
12 XXX  AAA 1H2016 1  1 
13 XXX  AAA 1H2017 0  0 
14 XXX  AAA 2H2016 0  1 
15 XXX  AAA 2H2017 0  0 
+0

酷....感謝它的工作它只是我的數據集有更遠的射程和我得到的列名作爲4我有水平將該列重命名爲.. –

相關問題