8

I have a pandas data frame and a numpy nd array with one dimension. Effectively it is a list.

How do I add a new column to the DataFrame with the values from the array?

test['preds'] = preds gives SettingWithCopyWarning And a warning:

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

And when I try pd.DataFrame({test,preds}) I get TypeError: unhashable type: 'list'

2
  • 2
    What is your original df? if test is already a df then test['preds']=preds would work. Commented Jun 28, 2015 at 18:11
  • 1
    To answer the SettingWithCopyWarning you need to post your original DataFrame Commented Jun 30, 2015 at 12:48

2 Answers 2

10

Thanks to EdChum the problem was this

test= DataFrame(test)
test['preds']=preds

It works!

Sign up to request clarification or add additional context in comments.

1 Comment

I get this warning: 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
0

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})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.