例如:
#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/