简体   繁体   中英

Can't insert column into a newly created dataframe (Python - Pandas)

Let df be a dataframe. I'm trying to place the column a into another dataframe called df2

 df2.insert(1,'a',df['a'])

Everything goes well when df2 is loaded from excel:

df2 = pandas.read_excel(filepath)

Yet, when I create df2 from scratch, there's an error:

df2 = pandas.DataFrame

The error reads:

insert() missing 1 required positional argument: 'value'

What is going on?

The correct way is that you give a value in pd.DataFrame(abc) now here abc can be a matrix or dictionary

you can use this too:

pd.DataFrame(df[['a']])

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