Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - Replace values based on index

If I create a dataframe like so:

import pandas as pd, numpy as np

df = pd.DataFrame(np.random.randint(0,100,size=(100, 2)), columns=list('AB'))

How would I change the entry in column A to be the number 16 from row 0 -15, for example? In other words, how do I replace cells based purely on index?

like image 480
Colin O'Brien Avatar asked Jun 09 '16 11:06

Colin O'Brien


People also ask

How do you replace a value in a DataFrame using an index?

You can easily replace a value in pandas data frames by just specifying its column and its index. Having the dataframe above, we will replace some of its values. We are using the loc function of pandas. The first variable is the index of the value we want to replace and the second is its column.

How do I change the values in pandas series based on conditions?

You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.

How do I change the index value of a column in pandas?

To reset the index in pandas, you simply need to chain the function . reset_index() with the dataframe object. On applying the . reset_index() function, the index gets shifted to the dataframe as a separate column.

How can I replace multiple values with one value in pandas?

Pandas replace multiple values in column replace. By using DataFrame. replace() method we will replace multiple values with multiple new strings or text for an individual DataFrame column. This method searches the entire Pandas DataFrame and replaces every specified value.


3 Answers

Use loc:

df.loc[0:15,'A'] = 16
print (df)
     A   B
0   16  45
1   16   5
2   16  97
3   16  58
4   16  26
5   16  87
6   16  51
7   16  17
8   16  39
9   16  73
10  16  94
11  16  69
12  16  57
13  16  24
14  16  43
15  16  77
16  41   0
17   3  21
18   0  98
19  45  39
20  66  62
21   8  53
22  69  47
23  48  53

Solution with ix is deprecated.

like image 64
jezrael Avatar answered Oct 14 '22 09:10

jezrael


In addition to the other answers, here is what you can do if you have a list of individual indices:

indices = [0,1,3,6,10,15]
df.loc[indices,'A'] = 16

print(df.head(16))

Output:

     A  B
0   16  4
1   16  4
2    4  3
3   16  4
4    1  1
5    3  0
6   16  4
7    2  1
8    4  4
9    3  4
10  16  0
11   3  1
12   4  2
13   2  2
14   2  1
15  16  1
like image 39
nischi Avatar answered Oct 14 '22 09:10

nischi


One more solution is

df.at[0:15, 'A']=16

print(df.head(20))

OUTPUT:

     A   B
0   16  44
1   16  86
2   16  97
3   16  79
4   16  94
5   16  24
6   16  88
7   16  43
8   16  64
9   16  39
10  16  84
11  16  42
12  16   8
13  16  72
14  16  23
15  16  28
16  18  11
17  76  15
18  12  38
19  91   6
like image 42
amandeep1991 Avatar answered Oct 14 '22 11:10

amandeep1991