I have a Pandas Dataframe with the following structure:
pd.DataFrame([None, '1 RB, 2 TE, 2 WR', '1 RB, 1 TE, 3 WR', '1 RB, 3 TE, 1 WR', '1 RB, 0 TE, 4 WR', '2 RB, 1 TE, 2 WR', '2 RB, 2 TE, 1 WR', '1 RB, 2 TE, 1 WR,1 P,2 LB,1 LS,3 DB', '6 OL, 2 RB, 2 TE, 0 WR'])
| RB |
|---|
| None |
| 1 RB, 2 TE, 2 WR |
| 1 RB, 1 TE, 3 WR |
| 1 RB, 1 TE, 3 WR |
| 1 RB, 0 TE, 4 WR |
Ideally, I would prefer to split the column into the following format:
| RB | TE | WR | P | LB | LS | DB | OL |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 2 | 2 | 0 | 0 | 0 | 0 | 0 |
| 1 | 1 | 3 | 0 | 0 | 0 | 0 | 0 |
| 1 | 3 | 1 | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 4 | 0 | 0 | 0 | 0 | 0 |
Where each of the original column values is parsed based on the label ("1 RB" would be the value 1 in the column "RB"). The pattern will always be [# position].
How would I accomplish this? Each column value in the original dataframe column is one long string, so it isn't already an array or something. Additionally, not every value in the original dataframe column follows the same order; i.e. there isn't a common pattern in the order of RB, TE, WR-- if there isn't a value, the string does not include "0 WR" for example.