在Pandas数据框中插入一列 [英] Insert a column to a pandas dataframe

查看:793
本文介绍了在Pandas数据框中插入一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景::我有一个代码,可以将Excel工作表中的数据读取到数据框中,合并到一个数据框中,并执行一些清理步骤.

问题:我正在尝试使用pd.insert将具有给定值的列添加到数据框的开头,但是每次运行此行时,数据框都会从变量资源管理器中消失. /p>

这是我正在使用的行:

fnl = fnl.insert(loc=1, column='N', value=15)

问题:为什么可能是这个原因,以及如何解决?

解决方案

pd.DataFrame.insert起作用并且不返回任何内容.您只需要

fnl.insert(1, 'N', 15)

因为它什么都不返回,所以您将None分配给了fnl,这就是为什么它消失了的原因.

Scenario: I have a code that reads data from excel worksheets into dataframes, merges into one dataframe, and perform some cleaning procedures.

Issue: I am trying to add a column with a given value to the beginning of the dataframe with pd.insert, but every time I run this line the dataframe disappears from the variable explorer.

This is the line I am using:

fnl = fnl.insert(loc=1, column='N', value=15)

Question: Why could be the reason for this, and how to fix it?

解决方案

pd.DataFrame.insert acts in place and doesn't return anything. All you need is

fnl.insert(1, 'N', 15)

Because it returns nothing, you were assigning None to fnl and that's why it was disappearing.

这篇关于在Pandas数据框中插入一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆