J Jones J Jones - 4 months ago 15
Python Question

Find and update row in Pandas DataFrame with non-unique index

I have a pandas DataFrame with a non-unique index, and I need to update one of the rows. I am able to update the row if I can index it numerically, as below:

array = np.random.randint(1, 10, (3, 3))
df = pd.DataFrame(array, index=['one', 'one', 'two'], columns=['col1', 'col2', 'col3'])

# df looks like this:
# col1 col2 col3
#one 2 3 6
#one 3 1 5
#two 4 5 2


new_data = pd.Series({'col1': 'new', 'col2': 'new', 'col3': 'new'})
df.iloc[0] = new_data


But what if I don't know the row number? Let's say I just want to set the first row with an index of 'one'. So, I want to find the slice using
.loc
and then assign my new value to the first row.

new_data = pd.Series({'col1': 'new', 'col2': 'new', 'col3': 'new'})
df.loc['one'][0] = new_data


However, this is assignment using chained indexing, which is not so good. Is there a different way to do this?

Answer

You can access it with df.iloc[(df.index == 'one').argmax()].

So your assignment would look like:

df.iloc[(df.index == 'one').argmax()] = new_data