使用多于一列的get

编程入门 行业动态 更新时间:2024-10-08 14:38:47
使用多于一列的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.0

Edit: 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)

更多推荐

本文发布于:2023-07-09 10:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085647.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!