从一个“单元”中将 pandas DataFrame中的对象移动到一个“单元”。到另一个 [英] Move object in pandas DataFrame from one "cell" to another
本文介绍了从一个“单元”中将 pandas DataFrame中的对象移动到一个“单元”。到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将pandas DataFrame中的对象从一个位置移动到另一个位置,并在不影响其他列的情况下将该对象的原始位置保留为空白。
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
谢谢!
推荐答案
使用:
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
这篇关于从一个“单元”中将 pandas DataFrame中的对象移动到一个“单元”。到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文