Reputation: 17282
I need to set the first row's value of a column for every group to False.
start_ix = group.head(1).index
print(start_ix)
Int64Index([20], dtype='int64')
print df.iloc[start_ix]['start']
20 False
Name: start, dtype: bool
df.iloc[start_ix]['start'] = True
But the value stays False
upon inspecting the DataFrame.
Upvotes: 3
Views: 14018
Reputation: 394041
So basically what you tried is what is called chained indexing and may or may not work:
df.iloc[start_ix]['start'] = True
It may help to think of this as chaining function calls, the problem here is that sometimes it returns a view which is what you intended, but other times it returns a copy resulting in no change in the original df.
So you should use ix
which allows you to be explicit in indexing by integer value (or label) and then specifying the column label you want to set the data:
df.ix[start_ix,'start'] = True
The docs detail the recommended methods, normally a warning will be raised when you are setting the value of a potential copy but it cannot catch all instances.
Update
Since version 0.20.0
ix
is deprecated in favour of loc
so the above becomes
df.loc[start_ix,'start'] = True
Upvotes: 3