This should be an easy question, but for some reason I can't find the answer online. I have a DataFrame column that consists of dummy variables:
这应该是一个简单的问题,但由于某种原因,我无法在线找到答案。我有一个由虚拟变量组成的DataFrame列:
import pandas as pd
foo = pd.Series([6,7,8,3])
foo1 = bob.apply(lambda x: bin(x)[2:].zfill(4))
foo1
0 0110
1 0111
2 1000
3 0011
What I want is a 4x4 data frame that looks like
我想要的是一个4x4数据框,看起来像
A B C D
0 1 1 0
0 1 1 1
1 0 0 0
0 0 1 1
I've tried using get_dummies to no results:
我尝试过使用get_dummies没有结果:
foo1.str.get_dummies()
0110 0111 1000 0011
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
str.split and making the column into a series of lists doesn't work either. What should I do?
str.split并使列成为一系列列表也不起作用。我该怎么办?
3 个解决方案
#1
#2
2
This will skip your initial step of foo
to foo1
and get you straight there from foo
这将跳过foo到foo1的初始步骤,并从foo直接到达那里
foo.apply(lambda x: pd.Series(list('{:04b}'.format(x))))
0 1 2 3
0 0 1 1 0
1 0 1 1 1
2 1 0 0 0
3 0 0 1 1
#3
2
In [49]: pd.DataFrame(foo1.apply(list).values.tolist())
Out[49]:
0 1 2 3
0 0 1 1 0
1 0 1 1 1
2 1 0 0 0
3 0 0 1 1
#1
3
You can try this:
你可以试试这个:
# convert the series to str type;
# extract all characters with regex .;
# unstack to wide format
foo1.astype(str).str.extractall('(.)')[0].unstack()
#2
2
This will skip your initial step of foo
to foo1
and get you straight there from foo
这将跳过foo到foo1的初始步骤,并从foo直接到达那里
foo.apply(lambda x: pd.Series(list('{:04b}'.format(x))))
0 1 2 3
0 0 1 1 0
1 0 1 1 1
2 1 0 0 0
3 0 0 1 1
#3
2
In [49]: pd.DataFrame(foo1.apply(list).values.tolist())
Out[49]:
0 1 2 3
0 0 1 1 0
1 0 1 1 1
2 1 0 0 0
3 0 0 1 1