Move Object In Pandas Dataframe From One "cell" To Another
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
Solution 1:
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 outputprint 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
Solution 2:
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
Post a Comment for "Move Object In Pandas Dataframe From One "cell" To Another"