将Pandas Dataframe列转换为一个热标签(Converting a Pandas Dataframe column into one hot labels)
我有一个与此类似的pandas数据帧:
Col1 ABC 0 XYZ A 1 XYZ B 2 XYZ C
通过在列ABC上使用熊猫
get_dummies()
函数,我可以得到这个:Col1 A B C 0 XYZ 1 0 0 1 XYZ 0 1 0 2 XYZ 0 0 1
虽然我需要这样的东西,ABC列有一个
list / array
数据类型:Col1 ABC 0 XYZ [1,0,0] 1 XYZ [0,1,0] 2 XYZ [0,0,1]
我尝试使用
get_dummies
函数,然后将所有列组合到我想要的列中。 我发现很多答案解释了如何将多个列组合成字符串,如下所示: 在pandas / python中的数据框中组合两列文本 。 但我无法想出将它们组合成列表的方式。这个问题介绍了使用sklearn的
OneHotEncoder
的想法,但我无法让它工作。 如何对一个pandas数据帧的一列进行单热编码?还有一件事:我遇到的所有答案都有解决方案,其中列名称必须在组合时手动输入。 有没有办法使用
Dataframe.iloc()
或拼接机制将列组合成一个列表?I have a pandas dataframe similar to this:
Col1 ABC 0 XYZ A 1 XYZ B 2 XYZ C
By using the pandas
get_dummies()
function on column ABC, I can get this:Col1 A B C 0 XYZ 1 0 0 1 XYZ 0 1 0 2 XYZ 0 0 1
While I need something like this, where the ABC column has a
list / array
datatype:Col1 ABC 0 XYZ [1,0,0] 1 XYZ [0,1,0] 2 XYZ [0,0,1]
I tried using the
get_dummies
function and then combining all the columns into the column which I wanted. I found lot of answers explaining how to combine multiple columns as strings, like this: Combine two columns of text in dataframe in pandas/python. But I cannot figure out a way to combine them as a list.This question introduced the idea of using sklearn's
OneHotEncoder
, but I couldn't get it to work. How do I one-hot encode one column of a pandas dataframe?One more thing: All the answers I came across had solutions where the column names had to be manually typed while combining them. Is there a way to use
Dataframe.iloc()
or splicing mechanism to combine columns into a list?
原文:https://stackoverflow.com/questions/47127388
最满意答案
- 父类中的
getS()
方法在Child类中继承,因此可用于Child对象。- 覆盖仅适用于方法而不适用于实例变量。
- 因此,即使您定义了具有相同名称的新变量,它也不会生效,因为它不会被覆盖
- Your
getS()
methods from parent class is inherited in Child class and hence it is available for Child object.- Overriding is only for methods and not for instance variables.
- So even if you define a new variable with same name, it will not take effect as it will not be overridden
相关问答
更多-
在QGraphicsScene中,mousePressEvent不能按预期工作(mousePressEvent not working as expected in QGraphicsScene)[2022-07-07]
这通过调用原始类函数而不是完全重写它来解决。 在mousePressEvent中: def mousePressEvent(self, e): print("press") if isinstance(self.parent, CreatePrefabGridWidget): self.setBrush(self.parent.cur_color) QGraphicsPolygonItem.mousePressEvent(self, e) This was fix ... -
如果父元素是固定的,则offset()。top不能按预期工作(offset().top is not working as expected in if parent element is fixed)[2022-09-26]
使用.position()获取元素相对于其偏移父元素的当前坐标。 .offset()方法为您提供相对于整个文档的坐标。 $(".child").position() Use .position() to get the current coordinates of an element relative to its offset parent. The .offset() method is gives you the coordinates relative to the entire documen ... -
Scala递归泛型:Parent [Child]和Child [Parent](Scala recursive generics: Parent[Child] and Child[Parent])[2023-05-17]
您可以使用http://programming-scala.labs.oreilly.com/ch13.html中给出的方法: abstract class ParentChildPair { type C <: Child type P <: Parent trait Child {self: C => def parent: P } trait Parent {self: P => def child: C } } class ActualParentChi ... -
你可以试试下面的动画代码: animations: [ trigger('openClose', [ state('open', style({ opacity: 1, visibility: 'visible', })), state('close', style({ opacity: 0, visibility: 'visible', })), // when we go from close to op ...
-
在你的滚动视图中试试android:fillViewport="true" 。 Try android:fillViewport="true" in your scrollview.
-
Jquery find()只有直接子节点不能按预期工作(Jquery find() only direct children doesn't work as expected)[2023-08-20]
编辑:忘记切换选项... 这是你的解决方案: https://jsfiddle.net/pfjsoL9t/2/ HTML: -
似乎根据此问题报告已经知道此问题,并且有一个解决它的拉取请求 。 作为一个短期修复,我遵循Brendon的建议发布到pull请求并通过声明自定义范围从而绕过scope_changed?绕过scope_changed? bug存在的方法。 希望这有助于将来节省一些时间和白发。 It appears as though this issue is already know according to this issue report and has a pull request addressing it. A ...
-
父类中的getS()方法在Child类中继承,因此可用于Child对象。 覆盖仅适用于方法而不适用于实例变量。 因此,即使您定义了具有相同名称的新变量,它也不会生效,因为它不会被覆盖 Your getS() methods from parent class is inherited in Child class and hence it is available for Child object. Overriding is only for methods and not for instance va ...
-
Node.appendChild返回附加到节点的子节点。 因此,在将文本节点附加到li ,将其附加到ul (从li删除) ipDOM=document.getElementById("ulref"); x = document.createElement('LI'); y = document.createTextNode("hello"); x.appendChild(y); // append the text node to the li ipDOM.appendChild(x); //append ...
-
在这里,您将事件处理程序设置为匿名函数,该函数返回对函数的引用: