2021年1月26日星期二

Pivot Pandas dataframe to see if condition is met

I have the following DataFrame that represents whether a User was present in some week, some year:

    User    Year    Week  0   John    2020    1  1   John    2020    2  2   Steve   2020    1  3   Fred    2020    3  4   George  2020    2     5   George  2020    3      ...     ...     ...  200 John    2021    2  201 John    2021    4  202 Steve   2021    2  203 Fred    2021    2  204 George  2021    1     205 George  2021    4  

I want to get a DataFrame that groups the dataset by User and each column represents whether he was present in a certain week of a certain year, each column either being of type boolean or integer with possible values 0 or 1.

It would look something like this:

        2020_1  2020_2  2020_3  ... 2021_1  2021_2  2021_3  2021_4  John         1       1       0  ...      0       1       0       1  Steve        1       0       0  ...      0       1       0       0  Fred         0       0       1  ...      0       1       0       0  George       0       1       1  ...      1       0       0       1  

Is there anyway to do this without iterating through the DataFrme?

Thanks.

https://stackoverflow.com/questions/65912371/pivot-pandas-dataframe-to-see-if-condition-is-met January 27, 2021 at 11:07AM

没有评论:

发表评论