python - Set value for particular cell in pandas DataFrame with iloc -


i have question similar this , this. difference have select row position, not know index.

i want df.iloc[0, 'col_name'] = x, iloc not allow kind of access. if df.iloc[0]['col_name] = x warning chained indexing appears.

for mixed position , index, use .ix. need make sure index not of integer, otherwise cause confusions.

df.ix[0, 'col_name'] = x 

update:

alternatively, try

df.iloc[0, df.columns.get_loc('col_name')] = x 

example:

import pandas pd import numpy np  # data # ======================== np.random.seed(0) df = pd.dataframe(np.random.randn(10, 2), columns=['col1', 'col2'], index=np.random.randint(1,100,10)).sort_index()  print(df)         col1    col2 10  1.7641  0.4002 24  0.1440  1.4543 29  0.3131 -0.8541 32  0.9501 -0.1514 33  1.8676 -0.9773 36  0.7610  0.1217 56  1.4941 -0.2052 58  0.9787  2.2409 75 -0.1032  0.4106 76  0.4439  0.3337  # .iloc get_loc # =================================== df.iloc[0, df.columns.get_loc('col2')] = 100  df        col1      col2 10  1.7641  100.0000 24  0.1440    1.4543 29  0.3131   -0.8541 32  0.9501   -0.1514 33  1.8676   -0.9773 36  0.7610    0.1217 56  1.4941   -0.2052 58  0.9787    2.2409 75 -0.1032    0.4106 76  0.4439    0.3337 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -