python - 在 python 中向数据帧添加新列时出现错误 "NameError: name ' newaxis' is not Defined

标签 python pandas dataframe

例如:

#Creating a new empty dataframe
test=pd.DataFrame()

test_list=[1,3,5,6,3,1]

#Now I am converting this list to a dataframe column
test["Test name"]=test_list

执行最后一行代码后,我收到此错误:

NameError: name 'newaxis' is not defined

最佳答案

您确定正确使用双引号吗?

这是我能够重现您的错误的方法之一:

import pandas as pd
test=pd.DataFrame()

test_list=[1,3,5,6,3,1]

test[newaxis]=test_list

这里的问题是你需要在 newaxis 上使用双引号,因为它应该是一个字符串:

import pandas as pd
test=pd.DataFrame()

test_list=[1,3,5,6,3,1]

test["newaxis"]=test_list

或者,如果您的 newaxis 是一个变量 - 那么您必须定义它:

import pandas as pd
test=pd.DataFrame()

test_list=[1,3,5,6,3,1]

newaxis = 'column_name'

test[newaxis]=test_list

此外 - 您可能会遇到与代码的其他部分相关的错误,例如,您尝试使用 numpy.newaxis,但您只是将其称为 newaxis 而不是 numpy.newaxis。此处需要有关错误的更多信息。

关于python - 在 python 中向数据帧添加新列时出现错误 "NameError: name ' newaxis' is not Defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59479233/

相关文章:

python - Keras 一个操作在 train_on_batch 时对梯度有 None

python - 根据其他数据帧值python为数据帧列赋予值

python - 使用 Python 将大于 70 MB 的文件上传到谷歌驱动器

python - 如何将 Pandas 系列或索引转换为 NumPy 数组?

python - 如何在 pandas 中保留不同日期的重复值

python - 将函数应用于数据框

python - 如何更改 pandas 数据框中的值?

python - 嵌套列表理解

python - 属性错误: 'DataFrame' object has no attribute 'nunique'

python - 如何使用python提高sql数据库的写入速度