如何将Python Pandas DataFrame中具有一列具有名称/值对的列转换为同一数据框中的其他列。
具有命名值对的列(attrs)具有类似:
的值
[{'attr_id': 7, 'val': '4.00'}, {'attr_id': 8, 'val': '2.50'}, {'attr_id': 9, 'val': '1750'}, {'attr_id': 11, 'val': 'false'}, {'attr_id': 10, 'val': 'false'}]
[{'attr_id': 7, 'val': '2.00'}, {'attr_id': 8, 'val': '1.00'}, {'attr_id': 11, 'val': 'false'}, {'attr_id': 10, 'val': 'false'}]
[{'attr_id': 11, 'val': 'false'}, {'attr_id': 10, 'val': 'false'}]
因此对于第一条记录,我尝试创建的新列将为attr_id7,attr_id8,attr_id9,attr_id10,attr_id11,其值分别为4.00、2.50、1750,false,false
考虑将列内容转换为适当的Python字典,然后使用类似答案Splitting dictionary/list inside a Pandas Column into Separate Columns
答案 0 :(得分:1)
也许类似于以下内容:
Declare Function HypMenuVRefresh Lib "HsAddin" () As Long
。
Application.ScreenUpdating = False
Dim list As Worksheet: Set list = ThisWorkbook.Worksheets("list")
Dim p As Worksheet: Set p = ThisWorkbook.Worksheets("p")
Dim calc As Worksheet: Set calc = ThisWorkbook.Worksheets("calc")
Dim cc As Worksheet: Set cc = ThisWorkbook.Worksheets("cc_act")
Dim cc_lr As Long
Dim calc_lr As Long: calc_lr = calc.Cells(Rows.Count, "A").End(xlUp).Row
Dim calc_lc As Long: calc_lc = calc.Cells(1,
calc.Columns.Count).End(xlToLeft).Column
Dim calc_rg As Range
Dim ctry_rg As Range
Dim i As Integer
Dim x As Integer
list.Activate
For x = 2 To Range("B" & Rows.Count).End(xlUp).Row
If list.Range("B" & x).Value <> "" Then
p.Cells(17, 3) = list.Range("B" & x).Value
End If
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
If list.Range("A" & i).Value <> "" Then
p.Cells(17, 4) = list.Range("A" & i).Value
p.Calculate
End If
p.Activate
Call HypMenuVRefresh
p.Calculate
'''changes country on calc table
calc.Cells(2, 2) = p.Cells(17, 4)
calc.Cells(2, 3) = p.Cells(17, 3)
calc.Calculate
'''copy the calc range and past under last column
With calc
Set calc_rg = calc.Range("A2:F2" & calc_lr)
End With
With cc
cc_lr = cc.Cells(Rows.Count, "A").End(xlUp).Row + 1
calc_rg.Copy
cc.Cells(cc_lr, "A").PasteSpecial xlPasteValues
End With
Next i
Next x
Application.ScreenUpdating = True
End Sub