[英]How to insert column on Python Pandas using dataframe.insert function?
I have been trying to insert a column to one of my data sets (A).我一直试图在我的一个数据集 (A) 中插入一列。 I have tried to use the pandas function Dataframe.insert, but it keeps on returning None to the data frame (code and results attached below).
我曾尝试使用 pandas function Dataframe.insert,但它继续返回 None 到数据帧(代码和结果附在下面)。 I have also tried to look online at some of the other examples but have not been able to identify any areas.
我还尝试在网上查看其他一些示例,但无法识别任何区域。 Could I seek your advice for anything I missed out or code I can input?
我可以就我错过的任何内容或我可以输入的代码征求您的意见吗?
I'm using Python 3.9, UTF-8.我正在使用 Python 3.9、UTF-8。 Thank you in advanced!
提前谢谢你!
>>> import pandas as pd
>>> A = pd.read_csv('input.csv')
#print(A)
>>> ID Date ... Result Test Date Full Text
0 A 4-Jan-00 ... NaN NaN
1 B 4-Jan-00 ... NaN NaN
2 C 4-Jan-00 ... NaN NaN
>>> B = A.insert(6, "Data Type", 1)
>>> print(B)
>>> None
Pandas' insert
method, inserts the requested column inplace . Pandas 的
insert
方法,在原地插入请求的列。 It doesn't return anything, however, you have tried to assign the non-existing output to another dataframe.它不返回任何内容,但是,您尝试将不存在的 output 分配给另一个 dataframe。 If you print
A
, you'll see that it has in fact at its 6th index, a column named Data Type
with the constant value of 1
.如果您打印
A
,您会看到它实际上在其第 6 个索引处有一个名为Data Type
的列,其常量值为1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.