从一个“单元”中将 pandas DataFrame中的对象移动到一个“单元”。到另一个

编程入门 行业动态 更新时间:2024-10-25 21:32:20
本文介绍了从一个“单元”中将 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中的对象移动到一个“单元”。到另一个

本文发布于:2023-11-29 18:23:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647225.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元   中将   对象   pandas   DataFrame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!