使用多于一列的get_dummies()(Using get_dummies() with more than a column)
我想用比get_dummies()更多的get_dummies() 。 ( 'CabinNumber', 'Name'包含字符串)当我删除其中一个并使用xtr = pd.get_dummies(x ['Name'])我的代码工作
我在这个答案中尝试了一切。 不过,我无法让我的代码工作。
x = df.loc[df['Price'].notnull(), ['Age','Fee', 'Size','Class','CabinNumber', 'Name' ]]我试过了:
xtr = pd.get_dummies(data=x, columns=['CabinNumber', 'Name'])我试过了:
xtr = pd.get_dummies(df.loc[df['Price'].notnull(), ['Age','Fee', 'Size','Class','CabinNumber', 'Name' ]])I want to use get_dummies() with more than a coulmn. ('CabinNumber', 'Name' contain strings ) when i delete one of them and use xtr = pd.get_dummies(x['Name']) my code works
I tried everything in this answer. However i could not get my code to work.
x = df.loc[df['Price'].notnull(), ['Age','Fee', 'Size','Class','CabinNumber', 'Name' ]]I tried:
xtr = pd.get_dummies(data=x, columns=['CabinNumber', 'Name'])I tried:
xtr = pd.get_dummies(df.loc[df['Price'].notnull(), ['Age','Fee', 'Size','Class','CabinNumber', 'Name' ]])最满意答案
我试图复制你的代码,我的工作正常。
data = {'a': ['foo', 'buzz'], 'b':['cookie', 'milk'], 'Price': ['super', ...: 'duper']} x = df.loc[df['Price'].notnull(), ['a', 'b']] >>> a b 0 foo cookie 1 buzz milk xtr = pd.get_dummies(data=x, columns = x.columns) xtr >>> a_buzz a_foo b_cookie b_milk 0 0.0 1.0 1.0 0.0 1 1.0 0.0 0.0 1.0编辑:你也可以按照你链接的线程来做到这一点
pd.concat([pd.get_dummies(x[col]) for col in x], axis=1, keys=x.columns)I tried replicating your code and mine works fine.
data = {'a': ['foo', 'buzz'], 'b':['cookie', 'milk'], 'Price': ['super', ...: 'duper']} x = df.loc[df['Price'].notnull(), ['a', 'b']] >>> a b 0 foo cookie 1 buzz milk xtr = pd.get_dummies(data=x, columns = x.columns) xtr >>> a_buzz a_foo b_cookie b_milk 0 0.0 1.0 1.0 0.0 1 1.0 0.0 0.0 1.0Edit: You could also do this as per the thread you linked
pd.concat([pd.get_dummies(x[col]) for col in x], axis=1, keys=x.columns)更多推荐
发布评论