1

I need to move an object in a pandas DataFrame from one location to another, and leave the original location of that object blank without affecting other columns.

employees = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
levels = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

df = pd.DataFrame({"Employees":employees, "Level":levels})

#move employee 1 from index 3 to index 1 without altering Level column
#leave index 3 blank after moving employee 1

Visually, I need to go from starting point to end point.

Thank you!

Charlie Roe
  • 89
  • 2
  • 8

2 Answers2

0

Here is my solution. You can just use the .loc function which takes the index as first argument and the column name as second argument.

Read more here

import pandas as pd
import numpy as np

employees = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
levels = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
df = pd.DataFrame({"Employees":employees, "Level":levels})

#substitution logic
temp = df['Employees'][3]
df.loc[3, 'Employees'] = ''
df.loc[1, 'Employees'] = temp
#print output
print df

OUTPUT:

 Employees  Level
0         0      1
1         1      2
2         0      3
3                4
4         0      5
5         0      6
6         0      7
7         0      8
8         0      9
9         0     10
Naren Murali
  • 12,508
  • 3
  • 19
  • 46
0

Use:

df = pd.DataFrame({"Employees":['','','','empployee_object','','','','','',''], 
                   "Level":range(1,11)}, index=range(1,11))
print (df)
           Employees  Level
1                         1
2                         2
3                         3
4   empployee_object      4
5                         5
6                         6
7                         7
8                         8
9                         9
10                       10

#move value from 4 index to 2 index in Employees column
df.loc[2, 'Employees'] = df.loc[4, 'Employees']
#set empty string to original
df.loc[4, 'Employees'] = ''
print (df)
           Employees  Level
1                         1
2   empployee_object      2
3                         3
4                         4
5                         5
6                         6
7                         7
8                         8
9                         9
10                       10
jezrael
  • 629,482
  • 62
  • 918
  • 895