Search code examples
pythonpandasdataframesumseries

Why is there a 0 in my header (on top of the indexes) after applying the .sum()?


This is my dataframe with i) columns representing the player names, ii) rows representing each game round, and iii) values representing whether they participated in each game round.

tdf = df[unique_players][mask]
tdf

              Player A | Player B | Player C | Player D | Player E
         10          0 |        1 |        1 |        0 |        1
         44          0 |        0 |        1 |        1 |        1
         45          0 |        0 |        1 |        1 |        1
         56          0 |        1 |        1 |        1 |        1

Somehow, when I apply tdf.sum(), I get a weird 0 on top of my index column. I also tried to convert into dataframe, or reset_index() and see if the 0 goes away, but it does not.

tdf.sum()

0                  << THIS IS THE WEIRD 0 
Player A        0
Player B        2
Player C        4
Player D        3
Player E        4

Tried to convert into a dataframe, but this is the output.

|          | 0 |
| 0        |   |
| -------- | - |
| Player A | 0 |
| Player B | 0 |
| Player C | 0 |
| Player D | 0 |
| Player E | 0 |

Real example of how it looks in my jupyter lab. Output snippet link below.

https://i.sstatic.net/sP4b1.png


Solution

  • Figured out why the issue was occurring, it was because I had a line of code df.columns = df.iloc[0] that causes this issue.

    Sample screenshot here to show the problem. https://i.sstatic.net/VDxMK.png