在 pandas.DataFrame 中添加一个 np.array 作为列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31103040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding an np.array as a column in a pandas.DataFrame
提问by DolphinGenomePyramids
I have a pandas data frame and a numpy nd array with one dimension. Effectively it is a list.
我有一个Pandas数据框和一个一维的 numpy nd 数组。实际上它是一个列表。
How do I add a new column to the DataFrame with the values from the array?
如何使用数组中的值向 DataFrame 添加新列?
test['preds'] = predsgives SettingWithCopyWarningAnd a warning:
test['preds'] = preds给出SettingWithCopyWarning并警告:
A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
试图在来自 DataFrame 的切片副本上设置值。尝试使用 .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'
回答by DolphinGenomePyramids
Thanks to EdChum the problem was this
感谢 EdChum 问题是这样的
test= DataFrame(test)
test['preds']=preds
It works!
有用!
回答by firelynx
This is not a pandas error, this error is because you are trying to instantiate a set with two lists.
这不是 Pandas 错误,这个错误是因为您试图用两个列表实例化一个集合。
{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.
话虽这么说,给大Pandas一套不会为你想要的结果工作。
Handing pandas a dict however, will work, like this:
但是,将 dict 交给 pandas 会起作用,如下所示:
pd.DataFrame({"test":test,"preds":preds})

