Set value for particular cell in pandas DataFrame using index

后端 未结 20 1572
野趣味
野趣味 2020-11-22 05:45

I\'ve created a Pandas DataFrame

df = DataFrame(index=[\'A\',\'B\',\'C\'], columns=[\'x\',\'y\'])

and got this

    x    y
A  NaN         


        
相关标签:
20条回答
  • 2020-11-22 06:24

    you can use .iloc.

    df.iloc[[2], [0]] = 10
    
    0 讨论(0)
  • 2020-11-22 06:25

    In my example i just change it in selected cell

        for index, row in result.iterrows():
            if np.isnan(row['weight']):
                result.at[index, 'weight'] = 0.0
    

    'result' is a dataField with column 'weight'

    0 讨论(0)
  • 2020-11-22 06:25

    If you want to change values not for whole row, but only for some columns:

    x = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
    x.iloc[1] = dict(A=10, B=-10)
    
    0 讨论(0)
  • 2020-11-22 06:26

    RukTech's answer, df.set_value('C', 'x', 10), is far and away faster than the options I've suggested below. However, it has been slated for deprecation.

    Going forward, the recommended method is .iat/.at.


    Why df.xs('C')['x']=10 does not work:

    df.xs('C') by default, returns a new dataframe with a copy of the data, so

    df.xs('C')['x']=10
    

    modifies this new dataframe only.

    df['x'] returns a view of the df dataframe, so

    df['x']['C'] = 10
    

    modifies df itself.

    Warning: It is sometimes difficult to predict if an operation returns a copy or a view. For this reason the docs recommend avoiding assignments with "chained indexing".


    So the recommended alternative is

    df.at['C', 'x'] = 10
    

    which does modify df.


    In [18]: %timeit df.set_value('C', 'x', 10)
    100000 loops, best of 3: 2.9 µs per loop
    
    In [20]: %timeit df['x']['C'] = 10
    100000 loops, best of 3: 6.31 µs per loop
    
    In [81]: %timeit df.at['C', 'x'] = 10
    100000 loops, best of 3: 9.2 µs per loop
    
    0 讨论(0)
  • 2020-11-22 06:27

    Try using df.loc[row_index,col_indexer] = value

    0 讨论(0)
  • 2020-11-22 06:30

    .iat/.at is the good solution. Supposing you have this simple data_frame:

       A   B   C
    0  1   8   4 
    1  3   9   6
    2  22 33  52
    

    if we want to modify the value of the cell [0,"A"] u can use one of those solution :

    1. df.iat[0,0] = 2
    2. df.at[0,'A'] = 2

    And here is a complete example how to use iat to get and set a value of cell :

    def prepossessing(df):
      for index in range(0,len(df)): 
          df.iat[index,0] = df.iat[index,0] * 2
      return df
    

    y_train before :

        0
    0   54
    1   15
    2   15
    3   8
    4   31
    5   63
    6   11
    

    y_train after calling prepossessing function that iat to change to multiply the value of each cell by 2:

         0
    0   108
    1   30
    2   30
    3   16
    4   62
    5   126
    6   22
    
    0 讨论(0)
提交回复
热议问题