[英]Adding an np.array as a column in a pandas.DataFrame
I have a pandas data frame and a numpy nd array with one dimension. 我有一个熊猫数据框和一个一维的numpy nd数组。 Effectively it is a list. 实际上,这是一个列表。
How do I add a new column to the DataFrame with the values from the array? 如何使用数组中的值向DataFrame添加新列?
test['preds'] = preds
gives SettingWithCopyWarning
And a warning: test['preds'] = preds
给出SettingWithCopyWarning
并给出警告:
A value is trying to be set on a copy of a slice from a DataFrame. 试图在DataFrame的切片副本上设置一个值。 Try using .loc[row_indexer,col_indexer] = value instead 尝试改用.loc [row_indexer,col_indexer] = value
And when I try pd.DataFrame({test,preds})
I get TypeError: unhashable type: 'list'
当我尝试pd.DataFrame({test,preds})
我得到TypeError: unhashable type: 'list'
Thanks to EdChum the problem was this 多亏了EdChum,问题是这样的
test= DataFrame(test)
test['preds']=preds
It works! 有用!
This is not a pandas error, this error is because you are trying to instantiate a set with two lists. 这不是大熊猫错误,此错误是因为您试图实例化具有两个列表的集合。
{test,preds}
#TypeError: unhashable type: 'list'
A set is a container which needs all its content to be hashable, since sets may not contain the same element twice. 集合是一个容器,它需要所有内容都是可散列的,因为集合可能不会两次包含相同的元素。
That being said, handing pandas a set will not work for your desired result. 话虽这么说,递给熊猫一套不能达到您想要的结果。
Handing pandas a dict however, will work, like this: 但是,将熊猫交给字典是可以的,就像这样:
pd.DataFrame({"test":test,"preds":preds})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.