[英]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.