2016-12-02 89 views
2

我的一个数据帧的小样本是在该格式分裂文本&使用熊猫

**shop** **product** **location** **time** **count_products** 
store1  ,A,B,C  X   8.30 pm  3 
store1  ,G,F   Y   8.41 pm  2 
store1  ,C,D,T,R  Z   9.02 pm  4 

现在我想分裂产物塔在python相应追加。我知道str.split可以分割特殊字符&,我可以分割列。输出我喜欢生成应具有以下格式,

**shop** **product** **location** **time** **count_products** 
store1  A    X   8.30 pm   3 
store1  B    X   8.30 pm   3 
store1  C    X   8.30 pm   3    
store1  G    Y   8.41 pm   2 
store1  F    Y   8.41 pm   2 
store1  C    Z   9.02 pm   4 
store1  D    Z   9.02 pm   4 
store1  T    Z   9.02 pm   4 
store1  R    Z   9.02 pm   4 

我使用熊猫& numpy的。你可以请指导我如何继续获得上述输出?提前致谢。

回答

3

可以使用str.strip用于去除,str.splitstack创建Seriesjoin原始DataFrame

reset_indexindex避免重复和重新排序列名由reindex_axis

print (
df.pop('**product**') 
.str 
.strip(',') 
.str 
.split(',',expand=True) 
.stack() 
.reset_index(drop=True, level=1) 
.rename('**product**')   
) 
0 A 
0 B 
0 C 
1 G 
1 F 
2 C 
2 D 
2 T 
2 R 
Name: **product**, dtype: object 
cols = df.columns 

print (df.join 
      (
      df.pop('**product**') 
      .str 
      .strip(',') 
      .str 
      .split(',',expand=True) 
      .stack() 
      .reset_index(drop=True, level=1) 
      .rename('**product**')   
      ).reset_index(drop=True) 
       .reindex_axis(cols,axis=1)) 

    **shop** **product** **location** **time** **count_products** 
0 store1   A   X 8.30 pm     3 
1 store1   B   X 8.30 pm     3 
2 store1   C   X 8.30 pm     3 
3 store1   G   Y 8.41 pm     2 
4 store1   F   Y 8.41 pm     2 
5 store1   C   Z 9.02 pm     4 
6 store1   D   Z 9.02 pm     4 
7 store1   T   Z 9.02 pm     4 
8 store1   R   Z 9.02 pm     4 
+0

哇..优秀 –

+0

我已经上投 –

+0

谢谢你,但你也可以点击清空在'1'下勾选绿色。 – jezrael