Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I'm unable to find out the mistake here in indexing. I am sure this must be some silly mistake. I want to set 'td' values of those rows to 0 whose 'block' size is 1. I'm first finding out such rows and then using those indices to set the values of the column 'td' to 0. Here is the sample data set. Here, except block no 5,7,8 all the other block values should be set to 0 in 'td' column.

    Sid     Itemid  Block       td

0     1  214536502      1  180.591

1     1  214536500      2    37.13

2     1  214536506      3  133.308

3     1  214577561      4      NaN

4     2  214662742      5   41.759

5     2  214662742      5   78.073

6     3  214576500      6      NaN

7     4  214821275      7   26.002

8     4  214821275      7   28.199

9     5  214821371      8   42.289

10    5  214821371      8   45.193

Here is my code. I'm getting unexpected output.

j=k.groupby('Block').Sid.count()==1

te=k['Block'][j[j].index].index

k['td'][te]=0

Expected Output-

    Sid     Itemid  Block       td

0     1  214536502      1       0

1     1  214536500      2       0

2     1  214536506      3       0

3     1  214577561      4       0

4     2  214662742      5   41.759

5     2  214662742      5   78.073

6     3  214576500      6       0

7     4  214821275      7   26.002

8     4  214821275      7   28.199

9     5  214821371      8   42.289

10    5  214821371      8   45.193

1 Answer

0 votes
by (41.4k points)

Use this below line of code:

k.ix[(k.groupby('Block').Sid.transform('count') == 1), 'td'] = 0

This will give you the expected output:

>>> k

    Sid     Itemid  Block      td

0     1  214536502      1   0.000

1     1  214536500      2   0.000

2     1  214536506      3   0.000

3     1  214577561      4   0.000

4     2  214662742      5  41.759

5     2  214662742      5  78.073

6     3  214576500      6   0.000

7     4  214821275      7  26.002

8     4  214821275      7  28.199

9     5  214821371      8  42.289

10    5  214821371      8  45.193

If you want to learn about Python Pandas visit this Python Pandas Tutorial.

Browse Categories

...