Adding series to Pandas dataframe yields column of NaN

Viewed 853

Using this data set (some cols and hundreds of rows omitted for brevity) . . .

    Year    Ceremony    Award          Winner   Name    
0   1927/1928   1       Best Actress    0.0     Louise Dresser  
1   1927/1928   1       Best Actress    1.0     Janet Gaynor
2   1937        10      Best Actress    0.0     Janet Gaynor
3   1927/1928   1       Best Actress    0.0     Gloria Swanson  
4   1929/1930   3       Best Actress    0.0     Gloria Swanson
5   1950        23      Best Actress    0.0     Gloria Swanson  

I used the following command . . .

ba_dob.loc[ba_dob.Winner == 0.0, :].groupby('Name').Winner.count()

To create the following series . . .

Name
Ali MacGraw                1
Amy Adams                  1
Angela Bassett             1
Angelina Jolie             1
Anjelica Huston            1
Ann Harding                1
Ann-Margret                1
Anna Magnani               1
Anne Bancroft              4
Anne Baxter                1
Anne Hathaway              1
Annette Bening             3
Audrey Hepburn             4

I tried adding the series to the original dataframe like so . . .

ba_dob['New_Col'] = ba_dob.loc[ba_dob.Winner == 0.0, :].groupby('Name').Winner.count()

I got an column of NaN values.

I've read the other posts suggesting that there might be some faulty indexing at work, but I'm not sure how that would shake out. More specifically, why would Pandas not be able to line up the indexes, as the groupby and count are coming from the same table. Is there something else afoot?

5 Answers
Related