I have a DataFrame containing strings. I would like to create another DataFrame that indicates whether the string contains a specific month through one-hot encoding.
Using the below as an example:
data = {
'User': ['1', '2', '3', '4']
'Months': ['January; February', 'March; August', 'October; January', 'August, December']}
df = pd.DataFrame(data, columns = ['User','Months'])
I am looking to produce the following sort of DataFrame:
| January | August |
User | 1 | 1 | 0 |
| 2 | 0 | 1 |
| 3 | 1 | 0 |
| 4 | 0 | 1 |
I have tried the following but I get a value error and it also would not produce the desired DataFrame:
if df[df['Months'].str.contains('January')]:
print("1")
else:
print("0")
Thanks in advance!