53

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?

6 Answers 6

90

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This now gives a FutureWarning: Slicing a positional slice with .loc is not supported. df.A.iloc[0:15] = 16 should still work.
31

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

Comments

8

Very interesting observation, that code below does change the value in the original dataframe

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

But if you use a pretty similar code like this

df.loc[0:15]['A'] = 16

Than it will give back just a copy of your dataframe with changed value and doesn't change the value in the original df object. Hope that this will save some time for someone dealing with this issue.

1 Comment

This is called "chained assignment" and the pandas documentation recommends against it (pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html)
7

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

Comments

0

Could you instead of 16, update the value of that column to -1.0? for me, it returns 255 instead of -1.0.

>>> effect_df.loc[3:5, ['city_SF', 'city_Seattle']] = -1.0

    Rent  city_SF  city_Seattle
0  3999        1             0
1  4000        1             0
2  4001        1             0
3  3499      255           255
4  3500      255           255
5  3501      255           255
6  2499        0             1
7  2500        0             1
8  2501        0             1

Comments

0

To Mad Physicist: it appears that at first you need to change the column data types from short integer to float. Looks like your -1.0 was cast as short integer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.