Pandas列dict拆分为新列和行

时间:2018-05-25 05:34:57

标签: python pandas

我在pandas dataframe列中有一个dict,输入是,

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Dropdowns</h2>
  <p>The .dropdown class is used to indicate a dropdown menu.</p>
  <p>Use the .dropdown-menu class to actually build the dropdown menu.</p>
  <p>To open the dropdown menu, use a button or a link with a class of .dropdown-toggle and data-toggle="dropdown".</p>                                          
  <div class="dropdown">
    <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example
    <span class="caret"></span></button>
    <ul class="dropdown-menu">
      <li><a href="#">HTML</a></li>
      <li><a href="#">CSS</a></li>
      <li class='divider'></li>
      <li><a href="#">JavaScript</a></li>
    </ul>
  </div>
</div>

</body>
</html>

我想将import pandas as pd df = pd.DataFrame([{'A': {'k1': 10}}, {'A': {'k2': 20, 'k3': 30}}, {'A': {'k4': 15}}]) df A 0 {u'k1': 10} 1 {u'k3': 30, u'k2': 20} 2 {u'k4': 15} 的键和值拆分为列&#39; A&#39;到新列并拆分为行(取决于dict中的键数),基本上输出应该是,

dict

2 个答案:

答案 0 :(得分:2)

使用带有flatenning的列表理解为元组,然后使用L = [(k1, v1) for k, v in df['A'].to_dict().items() for k1, v1 in v.items()] df = pd.DataFrame(L, columns = ['keys','values']) print (df) keys values 0 k1 10 1 k2 20 2 k3 30 3 k4 15 contructor:

DataFrame

或创建df = (pd.DataFrame(df['A'].values.tolist()) .stack().reset_index(level=0, drop=True) .reset_index()) df.columns = ['keys','values'] print (df) keys values 0 k1 10.0 1 k2 20.0 2 k3 30.0 3 k4 15.0 stack

select hadith_raw_ar from view_hadith_in_book where hadith_raw_ar like '%[بل]ت';

答案 1 :(得分:1)

选项1 (如果您在子词典中拥有所有唯一键) 使用 dict

collections.ChainMap
from collections import ChainMap   
dct = dict(ChainMap(*[i['A'] for i in d]))
pd.DataFrame(list(dct.items()), columns=['key', 'value'])

  key  value
0  k1     10
1  k4     15
2  k2     20
3  k3     30

选项2 (如果您的密钥可能重复)
itertools.chain.from_iterable

dct = list(itertools.chain.from_iterable([i['A'].items() for i in d]))
df = pd.DataFrame(dct, columns=['key', 'value'])

  key  value
0  k1     10
1  k2     20
2  k3     30
3  k4     15