Phurich.P
Phurich.P

Reputation: 1416

Shifting Column to the left Pandas Dataframe

I have a fruit dataset that contains Name, Colour, Weight, Size, Seeds

         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  

The problem is that, the colour column is a duplicated column of name and the values are shifted 1 column to the right, creating a useless column (Unnamed) that contains values that belong to column Seeds. Is there a easy way to remove the duplicated values in Colour and shift back the rest of the column values 1 column to the left from weight onwards. I hope i am not confusing anyone here.

Desire result

         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  

Upvotes: 6

Views: 14540

Answers (3)

tanesya
tanesya

Reputation: 101

You can use pandas shift: df.shift(-1, axis=1)

Example on df:

df = pd.DataFrame({
    "Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
    "Amount": [4, 1, 2, 2, 4, 5],
    "City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"]
})

# generalize all column as object to prevent forcing NaN due to incompatible dtypes
df[df.columns] = df[df.columns].astype('object')

# shift: to shift the value
# dropna(axis=1): to drop column with NaN result 
df.shift(-1, axis=1).dropna(axis=1)

Upvotes: 1

Aakash Saxena
Aakash Saxena

Reputation: 309

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

Upvotes: 0

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210842

you can do it this way:

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

Upvotes: 5

Related Questions