简体   繁体   English

无法将列插入新创建的 dataframe(Python - Pandas)

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

Let df be a dataframe.df为 dataframe。 I'm trying to place the column a into another dataframe called df2我正在尝试将列a放入另一个名为df2的 dataframe

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

Everything goes well when df2 is loaded from excel:从 excel 加载 df2 时一切顺利:

df2 = pandas.read_excel(filepath)

Yet, when I create df2 from scratch, there's an error:然而,当我从头开始创建 df2 时,出现了一个错误:

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正确的方法是你现在在pd.DataFrame(abc)中给出一个值,这里abc可以是矩阵或字典

you can use this too:你也可以使用它:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从json创建的Pandas数据框有未命名的列 - 由于未命名的列问题而无法插入MySQL - Pandas dataframe created from json has unnamed column - can't insert into MySQL due to unnamed column issue 如何将值插入到新创建的空数据框的列中? - How to insert values into a column of an empty newly created dataframe? 如何为新创建的数据框熊猫分配列名 - How to assign column names to newly created dataframe pandas 如何使用 Python Pandas 将每行中列的前四个元素和 Append 提取到新创建的列? - How Can I Take The First Four Elements Of A Column In Each Row and Append it To A Newly Created Column Using Python Pandas? 将 1 的随机数插入一列 Pandas 数据框 python - Insert 1's random into a column pandas dataframe python Python - Pandas 在 Dataframe 中插入列一次 - Python - Pandas Insert Column in Dataframe Once 将列插入pandas数据帧 - Insert a column to a pandas dataframe Pandas:如何使用现有列和新创建列中的先前行创建新列? - Pandas : How can I create new column using previous rows from existing column and newly created column? 如何使用 dataframe.insert ZC1C425268E68385D1AB5074 在 Python Pandas 上插入列? - How to insert column on Python Pandas using dataframe.insert function? 如何将 pandas dataframe output 保存到新创建的文件夹中的 ZCC8D68C551C4A9A6D5313EDZDE 文件中? - How can I save pandas dataframe output to a CSV file in a newly created folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM