Considering that the original dataframe is df, one can use:
pandas.get_dummies
pandas.Series.str.get_dummies
Option 1
Using pandas.get_dummies, one can do the following
df2 = pd.get_dummies(df['my_col'], dtype=bool)
[Out]:
green red
0 False True
1 False True
2 True False
If one wants the column red to appear first, a one-liner would look like the following
df2 = pd.get_dummies(df['my_col'], dtype=bool)[['red', 'green']]
[Out]:
red green
0 True False
1 True False
2 False True
Option 2
Using pandas.Series.str.get_dummies, one can do the following
df2 = df['my_col'].str.get_dummies().astype(bool)
[Out]:
green red
0 False True
1 False True
2 True False
If one wants the column red to appear first, a one-liner would look like the following
df2 = df['my_col'].str.get_dummies().astype(bool)[['red', 'green']]
[Out]:
red green
0 True False
1 True False
2 False True