Bondrak
Bondrak

Reputation: 1590

python 3, pandas dataframes

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?

Upvotes: 0

Views: 37

Answers (2)

jpp
jpp

Reputation: 164673

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

df = pd.DataFrame({'CLASS': [1]*50 + [0]*50})

Upvotes: 1

Allen Qin
Allen Qin

Reputation: 19947

You can do:

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

Upvotes: 1

Related Questions