In [54]:
import pandas as pd
from pandas.api.types import CategoricalDtype
In [55]:
pd.__version__
Out[55]:
In [56]:
df = pd.DataFrame({'country': ['russia', 'germany', 'australia','korea','germany']})
df
Out[56]:
In [57]:
df = pd.DataFrame({'country': ['russia', 'germany', 'australia','korea','germany']})
pd.get_dummies(df,prefix=['country'])
Out[57]:
In [58]:
df = pd.DataFrame({'country': ['russia', 'germany', 'australia','korea','germany']})
pd.get_dummies(df["country"], prefix='country', drop_first=True)
Out[58]:
In [59]:
df = pd.DataFrame({'country': ['russia', 'germany', 'australia','korea','germany']})
df["country"] = df["country"].astype(CategoricalDtype(["australia","germany","korea","russia","japan"]))
In [60]:
df
Out[60]:
In [61]:
pd.get_dummies(df["country"],prefix='country')
Out[61]:
In [62]:
import pandas as pd
# df now has two columns: name and country
df = pd.DataFrame({
'name': ['josef','michael','john','bawool','klaus'],
'country': ['russia', 'germany', 'australia','korea','germany']
})
# use pd.concat to join the new columns with your original dataframe
df = pd.concat([df,pd.get_dummies(df['country'], prefix='country')],axis=1)
# now drop the original 'country' column (you don't need it anymore)
df.drop(['country'],axis=1, inplace=True)
In [63]:
df
Out[63]:
In [64]:
import numpy as np
In [65]:
df = pd.DataFrame({
'country': ['germany',np.nan,'germany','united kingdom','america','united kingdom']
})
df
Out[65]:
In [66]:
pd.get_dummies(df["country"],dummy_na=True)
Out[66]:
In [67]:
df = pd.DataFrame({
'has_dogs':[True,False,True,True,False,True],
'country': ['germany',np.nan,'germany','united kingdom','america','united kingdom']
})
In [68]:
pd.get_dummies(df)
Out[68]:
In [69]:
df = pd.DataFrame({'country': ['russia', 'germany', 'australia','korea','germany']})
pd.get_dummies(df,prefix=['country'], drop_first=True)
Out[69]: