从嵌套列表制作数据框

时间:2018-07-29 09:54:01

标签: python list pandas dataframe

希望进行转换:

<div class="accordion shadowed-box shipping collapsed summary">
  <fieldset>
    <legend id="legend1">
    Shipping
        <div id="shippingTooltip" class="form-field-tooltip cvnship-tip" role="tooltip">
          <span class="tooltip">
            <div class="tooltip-content" data-layout="small tooltip-cvn">
              <div id="cart-checkout-shipping-tooltip" class="html-slot-container">
                <p>We ship UPS, FedEx and/or USPS Priority Mail.<br>  
                  <a class="dialogify" data-dlg-options="{&quot;height&quot;:200}" href="https://www.payless.com/customer-service/ordering-and-shipping/cs-ordering-shipping-schedule.html" title="shipping information">Learn more about our shipping methods and prices.</a>
                </p>    
              </div> 
            </div>
          </span>
        </div>
      <a href="javascript:app.checkout.editShipping();" id="section-header-note-sa" class="section-header-note" style="display: inline;">Edit</a>
    </legend>
  </fieldset>
</div>

...如下所示进入数据框:

nested_list = 
[('R1',
  {'a', 'b', 'c'},
  {20.0,   40.0,   50.0,   60.0,   750.0}),
 ('R2',
  {'x', 'y', 'z'},
  {35.0,   37.5,   165.0}), 
 ('R3',
  {'x', 'a', 'm'},
  {2.5,   5.0,   7.5,   10.0,   12.5,   45.0})]

列表的每一行(例如R1)都有一组读数(如Cat Column Value --- ------ ----- R1 a 20.0 R1 a 40.0 R1 a 50.0 R1 a 60.0 R1 b 20.0 R1 b 40.0 ... R3 m 12.5 R3 m 45.0 )和一组元素({20.0, 40.0...})。读数和元素大小不相等。

2 个答案:

答案 0 :(得分:2)

在给定输入列表itertools的情况下,这是使用L的一种方法:

from itertools import chain, product, repeat

col, value = zip(*(list(i) for item in L for i in product(item[1], item[2])))
cat = list(chain.from_iterable(repeat(i, len(j) * len(k)) for i, j, k in L))

df = pd.DataFrame({'Cat': cat, 'Column': col, 'Value': value})
df = df.sort_values(['Cat', 'Column', 'Value']).reset_index(drop=True)

print(df)

   Cat Column  Value
0   R1      a   20.0
1   R1      a   40.0
2   R1      a   50.0
3   R1      a   60.0
4   R1      a  750.0
5   R1      b   20.0
...
39  R3      x   10.0
40  R3      x   12.5
41  R3      x   45.0

答案 1 :(得分:1)

首先从第一个元素创建列表,然后按0展开,并在必要时添加product

sorted