Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indexing Error in Pandas

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
like image 296
ADITYA AWALKAR Avatar asked Dec 01 '25 22:12

ADITYA AWALKAR


1 Answers

This is how you would do the assignment:

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

>>> 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

Transform returns a series the same length as the dataframe. Then find find those that equal one, and use loc to set the column td at those index locations to the value of zero.

like image 86
Alexander Avatar answered Dec 03 '25 12:12

Alexander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!