简体   繁体   中英

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). 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). 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. 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 . It doesn't return anything, however, you have tried to assign the non-existing output to another 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM