我有一个数据框
Index Cat Prod1 Prod2 Prod2
'Pens' 'Writing' 'Red' 'Blue' 'Green'
我正尝试使用to.dict()
将该字典放入以下格式:
dic= {'Pens':['Writing',['Red','Blue','Green']]}
这可能吗?
答案 0 :(得分:2)
想法将前两列转换为MultiIndex
,然后使用dict comprehension
:
a = df.set_index(['Index','Cat'])
d = {i: [j, list(v)] for (i, j), v in a.T.items()}
print (d)
{'Pens': ['Writing', ['Red', 'Blue', 'Green']]}
答案 1 :(得分:0)
有可能。
第一步是将所有 Prod ... 列“分组”到一个列表中(每行), 创建 Prod 列。
node {
def previousStageDuration = 0
def currentStageDuration = 0
previousStageDuration = currentBuild.duration
currentStageDuration = 0
timeout(time: 2, unit: 'SECONDS') {
retry(2) {
currentStageDuration = currentBuild.duration - previousStageDuration
// currentStageDuration is in milliseconds
if(currentStageDuration/1000 >= 2)
{
currentBuild.result = 'ABORTED'
error('Timed out. Ideally, the retry attempt should not have happened.')
}
parallel(
a: {
build job: 'Child_Job',
parameters: [
string(name: 'Param1', value: 'Val1'),
string(name: 'Param2', value: 'Val2')
]
)
}
}
}
然后:
执行此操作的代码是:
df['Prod'] = df.iloc[:, 2:].apply(list, axis=1)