Home > Mobile >  How to set values in a dataframe based on rows?
How to set values in a dataframe based on rows?

Time:11-02

I have a dataframe like this:

id  value  
111  0.222 
222  2.253
333  0.444
444  21.256
....

I want to add a new column flag and the first half of the rows, set flag to 0, the rest is set to 1:

id         value    flag 
111        0.222    0 
222        2.253    0
333        0.444    0
444       21.256    0
...
8888     1212.500   1
9999     0.025      1

What's the best way to do this? I tired the following:

df['flag'][:int(len(df) / 2)] = 0
df['flag'][int(len(df) / 2):] = 1

But this gave me KeyError: 'flag', assuming probably I need to create an empty column with name flag? Can someone help please? Thanks.

CodePudding user response:

Even if you create an empty column, you would get some warning/error due to chain indexing. Try assign at once:

df['flag'] = (np.arange(len(df)) >= (len(df)//2)).astype(int)

Or

l = len(df)//2
df['flag'] = [0] * s   [1] * (len(df) - l)
  • Related