DBA108642
DBA108642

Reputation: 2112

How to move data in dataframe column to the right

I have a dataframe like so:

|order_id|channel|category      |email   |
-----------------------------------------
|1234567 |email  |[email protected]|NaN     |
|3454884 |print  |[email protected]|NaN     |
|2357457 |other  |[email protected]|NaN     |
|5679465 |email  |[email protected]|NaN     |

where data has been incorrectly shifted to the left one row. this is because category used to be a field we tracked but it has since been removed but sometimes we still receive messed up files. I need to shift the contents of the category column into the email column so I'd have a df like this:

|order_id|channel|category|email         |
------------------------------------------
|1234567 |email  |NaN     |[email protected]|
|3454884 |print  |NaN     |[email protected]|
|2357457 |other  |NaN     |[email protected]|
|5679465 |email  |NaN     |[email protected]|

it's ok that the Category column is null; there are other fields that follow the email field that need to have the shift to the right as well.

Is there an easy way to do this? Doesn't really need to be repeatable, this is an ad hoc thing

Upvotes: 0

Views: 387

Answers (2)

ansev
ansev

Reputation: 30920

Use pd.Index.union

df.columns = df.columns[:-2].union(df.columns[::-1],sort=False)

Example

print(df.columns)
df.columns = df.columns[:-2].union(df.columns[::-1],sort=False)
print(df.columns)

#Index(['date', 'alkalinity', 'pH'], dtype='object')
#Index(['date', 'pH', 'alkalinity'], dtype='object')

Upvotes: 1

Celius Stingher
Celius Stingher

Reputation: 18367

For this particular case you can simply consider renaming the columns.

df.columns = ['order_id','channel','category','email']

Or if the list is too long and you wish to address only those two columns:

df = df.rename(columns={df.columns[2]:'email',df.columns[3]:'category')

Example:

data = {'category':['A','B','C','D','E'],'email':[0.001,0.2,0.2,0.2,0.3]}
df = pd.DataFrame(data)
df = df.rename(columns={df.columns[0]:'factor',df.columns[1]:'c1'})
print(df)

Output:

  email  category
0     A     0.001
1     B     0.200
2     C     0.200
3     D     0.200
4     E     0.300

Upvotes: 2

Related Questions