0

I have a dataframe and I need to create a new column and assign values. if say there are 100 rows in that column, I want to be able to say that the first 50 are assigned value 1 and the rest are 0. I know that in order to create a new column, I could do df['CLASS'] = 0, but in this case, all values are 0s. How should I modify this statement to be able to assign different values?

2 Answers 2

1

You can assign values when you create the dataframe via a dictionary, as so:

df = pd.DataFrame({'CLASS': [1]*50 + [0]*50})
Sign up to request clarification or add additional context in comments.

Comments

1

You can do:

df = pd.DataFrame(np.random.rand(100,3))
df['CLASS']=[1]*50 + [0]*50

2 Comments

What's np.random.rand(100,3) for?
Just to create some dummy data in the DataFrame

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.