I have two index-related questions on Python Pandas dataframes.
import pandas as pd
import numpy as np
df = pd.DataFrame({'id' : range(1,9),
'B' : ['one', 'one', 'two', 'three',
'two', 'three', 'one', 'two'],
'amount' : np.random.randn(8)})
df = df.ix[df.B != 'three'] # remove where B = three
df.index
>> Int64Index([0, 1, 2, 4, 6, 7], dtype=int64) # the original index is preserved.
1) I do not understand why the indexing is not automatically updated after I modify the dataframe. Is there a way to automatically update the indexing while modifying a dataframe? If not, what is the most efficient manual way to do this?
2) I want to be able to set the B
column of the 5th element of df
to 'three'. But df.iloc[5]['B'] = 'three'
does not do that. I checked on the manual but it does not cover how to change a specific cell value accessed by location.
If I were accessing by row name, I could do: df.loc[5,'B'] = 'three'
but I don't know what the index access equivalent is.
P.S. Link1 and link2 are relevant answers to my second question. However, they do not answer my question.