2016-09-17 125 views
3

我有一個水果數據集,其中包含名稱,顏色,重量,尺寸,種子轉移列向左大熊貓據幀

  Fruit dataset 

     Name  Colour Weight Size Seeds Unnamed 

     Apple Apple  Red  10.0 Big  Yes 

     Apple Apple  Red  5.0 Small Yes 

     Pear  Pear  Green 11.0 Big  Yes 

     Banana Banana Yellow 4.0 Small Yes 

     Orange Orange Orange 5.0 Small Yes 

的問題是,顏色列名和值的重複列向右移動1列,創建一個無用的列(未命名),其中包含屬於列種子的值。是否有一種簡單的方法可以在Color中刪除重複的值,並將權重向後的列值的其餘部分向左移1列。我希望我不會在這裏混淆任何人。

慾望結果

  Fruit dataset 

     Name  Colour Weight Size Seeds Unnamed(will be dropped) 

     Apple Red  10.0 Big  Yes 

     Apple Red  5.0 Small Yes 

     Pear  Green 11.0 Big  Yes 

     Banana Yellow 4.0 Small Yes 

     Orange Orange 5.0 Small Yes 

回答

3

你能做到這樣:

In [23]: df 
Out[23]: 
    Name Colour Weight Size Seeds Unnamed 
0 Apple Apple  Red 10.0 Big  Yes 
1 Apple Apple  Red 5.0 Small  Yes 
2 Pear Pear Green 11.0 Big  Yes 
3 Banana Banana Yellow 4.0 Small  Yes 
4 Orange Orange Orange 5.0 Small  Yes 

In [24]: cols = df.columns[:-1] 

In [25]: cols 
Out[25]: Index(['Name', 'Colour', 'Weight', 'Size', 'Seeds'], dtype='object') 

In [26]: df = df.drop('Colour', 1) 

In [27]: df.columns = cols 

In [28]: df 
Out[28]: 
    Name Colour Weight Size Seeds 
0 Apple  Red 10.0 Big Yes 
1 Apple  Red  5.0 Small Yes 
2 Pear Green 11.0 Big Yes 
3 Banana Yellow  4.0 Small Yes 
4 Orange Orange  5.0 Small Yes 
+0

非常感謝你。 –

+0

@ Niche.P,歡迎您:) – MaxU

0

If you would like to shift the columns without changing the contents in the column then user EdChum has resolved. See below or Click here.

In: 
df = pd.DataFrame({'a':randn(3), 'b':randn(3), 'c':randn(3)}) 
df 

Out: 
      a   b   c 
0 -0.682446 -0.200654 -1.609470 
1 -1.998113 0.806378 1.252384 
2 -0.250359 3.774708 1.100771 
In: 
cols = list(df) 
cols[1], cols[0] = cols[0], cols[1] 
cols 

Out: 
['b', 'a', 'c'] 

In: 
df = df.ix[:,cols] 

Out: 
      b   a   c 
0 -0.200654 -0.682446 -1.609470 
1 0.806378 -1.998113 1.252384 
2 3.774708 -0.250359 1.100771