嗨,我在根据值列旋转表时遇到问题。
假设我们有一个多索引数据框import re
for item in listA:
if re.search(r"\b{}\b".format(item), sentence.lower().strip()):
print('success')
else:
print('Not Present')
:
其索引为grade
,Country
,Date
和列Group
Status
我想堆叠 Status
Country Date Group
US 2019-12-31 Group A Absent
Group B Not Pass
Group C Absent
2020-01-02 Group A Pass
Group B Pass
Group C Pass
... ... ... ...
ID 2020-04-14 Group A Pass
Group B Pass
Group C Pass
2020-04-15 Group A Pass
Group B Pass
Group C Pass
和group
列,并根据Status
列创建清单。
因此,最后,我们得到了一个新数据框Status
,其中每个列checklist_grade
的列Absent
,Not Pass
,Pass
和值{{1} }在相应的group
值列中。
为便于理解我们想要的插图,
v
我正在尝试拆开status
数据框,但是它只会分解直到 Status
Group A Group B Group C
Country Date Absent Not Pass Pass Absent Not Pass Pass Absent Not Pass Pass
US 2019-12-31 v v v
2020-01-02 v v v
... ... ... ... ... ... ... ... ... ... ...
ID 2020-04-14 v v v
2020-04-15 v v v
:
grade
答案 0 :(得分:0)
创建新列,将<?php
return [
"fixtures" => [
[
"cats" => [
[
'id' => 1,
"title" => 'qwer',
],
[
'id' => 2,
"title" => 'asdf',
]
]
],
"dogs" => [
[
'id' => 1,
"title" => 'qwer',
],
],
"pigs" => [
[
'id' => 1,
"title" => 'qwer',
],
],
]
]
转换为Status
并通过DataFrame.unstack
进行重塑:
MultiIndex
在必要时最后将df = (df.assign(New='v')
.set_index('Status', append=True)
.unstack([2,3])
.rename(columns={'New':'Status'}))
print (df)
Status
Group Group A Group B Group C Group A Group B Group C
Status Absent Not Pass Absent Pass Pass Pass
Country Date
ID 2020-04-14 NaN NaN NaN v v v
2020-04-15 NaN NaN NaN v v v
US 2019-12-31 v v v NaN NaN NaN
2020-01-02 NaN NaN NaN v v v
中的所有组合级别加DataFrame.reindex
和MultiIndex.from_product
:
MultiIndex