将 pandas 中的融化数据重塑为宽 table,其中 True/False 作为变量具有值的值
Reshape melted data to wide table in pandas with True/False as values where variable has value
给定数据如下:
x
y
a
d1
a
d2
b
d1
b
d3
想创建:
thing
d1
d2
d3
a
True
True
False
b
True
False
True
示例数据:
data_from_csv = 'x,y\na,d1\na,d2\nb,d1\nb,d3\n'
data_to_csv = 'thing,d1,d2,d3\na,True,True,False\nb,True,False,True\n'
----------
I thought there was some sort of unstacking which could be done here but I can't seem to get it working.
使用 crosstab
测试不等于 0
:
df = pd.crosstab(df['x'], df['y']).ne(0)
print (df)
y d1 d2 d3
x
a True True False
b True False True
df=pd.crosstab(df['x'],df['y']).ne(0).rename_axis(index='thing',columns=None).reset_index()
print (df)
thing d1 d2 d3
0 a True True False
1 b True False True
给定数据如下:
x | y |
---|---|
a | d1 |
a | d2 |
b | d1 |
b | d3 |
想创建:
thing | d1 | d2 | d3 |
---|---|---|---|
a | True | True | False |
b | True | False | True |
示例数据:
data_from_csv = 'x,y\na,d1\na,d2\nb,d1\nb,d3\n'
data_to_csv = 'thing,d1,d2,d3\na,True,True,False\nb,True,False,True\n'
----------
I thought there was some sort of unstacking which could be done here but I can't seem to get it working.
使用 crosstab
测试不等于 0
:
df = pd.crosstab(df['x'], df['y']).ne(0)
print (df)
y d1 d2 d3
x
a True True False
b True False True
df=pd.crosstab(df['x'],df['y']).ne(0).rename_axis(index='thing',columns=None).reset_index()
print (df)
thing d1 d2 d3
0 a True True False
1 b True False True